From bd9a61684d0bc7618483a972ea835440a7d1ca44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C4=83zvan=20Biri=C8=99an?= Date: Sun, 7 Sep 2025 18:52:24 +0300 Subject: [PATCH 1/6] Add `Html::colorInput()` and `Input::color()` --- src/Html.php | 20 ++++++++++++++++++++ src/Tag/Input.php | 21 +++++++++++++++++++++ src/Tag/Input/Color.php | 20 ++++++++++++++++++++ tests/HtmlTest.php | 16 ++++++++++++++++ tests/Tag/Input/ColorTest.php | 22 ++++++++++++++++++++++ tests/Tag/InputTest.php | 8 ++++++++ 6 files changed, 107 insertions(+) create mode 100644 src/Tag/Input/Color.php create mode 100644 tests/Tag/Input/ColorTest.php diff --git a/src/Html.php b/src/Html.php index e8b502af..dff7ad49 100644 --- a/src/Html.php +++ b/src/Html.php @@ -35,6 +35,7 @@ use Yiisoft\Html\Tag\Img; use Yiisoft\Html\Tag\Input; use Yiisoft\Html\Tag\Input\Checkbox; +use Yiisoft\Html\Tag\Input\Color; use Yiisoft\Html\Tag\Input\File; use Yiisoft\Html\Tag\Input\Radio; use Yiisoft\Html\Tag\Input\Range; @@ -783,6 +784,7 @@ public static function textInput( return $attributes === [] ? $tag : $tag->addAttributes($attributes); } + /** * Generates a hidden input field. * @@ -877,6 +879,24 @@ public static function range( return $attributes === [] ? $tag : $tag->addAttributes($attributes); } + /** + * Generates a color {@see Input} field. + * + * @see Input::color() + * + * @param string|null $name The name attribute. + * @param string|Stringable|null $value The value attribute. + * @param array $attributes The tag attributes in terms of name-value pairs. + */ + public static function colorInput( + ?string $name = null, + string|Stringable|null $value = null, + array $attributes = [] + ): Color { + $tag = Input::color($name, $value); + return $attributes === [] ? $tag : $tag->addAttributes($attributes); + } + /** * Generates a checkbox {@see Input}. * diff --git a/src/Tag/Input.php b/src/Tag/Input.php index d8a1f42a..9e50aeb5 100644 --- a/src/Tag/Input.php +++ b/src/Tag/Input.php @@ -7,6 +7,7 @@ use Stringable; use Yiisoft\Html\Tag\Base\InputTag; use Yiisoft\Html\Tag\Input\Checkbox; +use Yiisoft\Html\Tag\Input\Color; use Yiisoft\Html\Tag\Input\File; use Yiisoft\Html\Tag\Input\Radio; use Yiisoft\Html\Tag\Input\Range; @@ -149,6 +150,26 @@ public static function range(?string $name = null, float|int|string|Stringable|n return $input; } + /** + * Color. + * + * @link https://html.spec.whatwg.org/multipage/input.html#color-state-(type=color) + * + * @param string|null $name Name of the input. + * @param string|Stringable|null $value Value of the input. + */ + public static function color(?string $name = null, string|Stringable|null $value = null): Color + { + $input = Color::tag(); + if ($name !== null) { + $input = $input->name($name); + } + if ($value !== null) { + $input = $input->value($value); + } + return $input; + } + /** * Button. * diff --git a/src/Tag/Input/Color.php b/src/Tag/Input/Color.php new file mode 100644 index 00000000..e75366b8 --- /dev/null +++ b/src/Tag/Input/Color.php @@ -0,0 +1,20 @@ +attributes['type'] = $this->getType(); + } + + protected function getType(): string + { + return 'color'; + } +} diff --git a/tests/HtmlTest.php b/tests/HtmlTest.php index 3c7c764a..eb3cdc38 100644 --- a/tests/HtmlTest.php +++ b/tests/HtmlTest.php @@ -382,6 +382,22 @@ public function testTextInput(): void ); } + public function testColorInput(): void + { + $this->assertSame('', Html::colorInput()->render()); + $this->assertSame('', Html::colorInput('')->render()); + $this->assertSame('', Html::colorInput(null, '')->render()); + $this->assertSame('', Html::colorInput('test')->render()); + $this->assertSame( + '', + Html::colorInput('test', '#ff0000')->render(), + ); + $this->assertSame( + '', + Html::colorInput('test', '#ff0000', ['required' => true])->render(), + ); + } + public function testHiddenInput(): void { $this->assertSame('', Html::hiddenInput()->render()); diff --git a/tests/Tag/Input/ColorTest.php b/tests/Tag/Input/ColorTest.php new file mode 100644 index 00000000..ae5a1d3d --- /dev/null +++ b/tests/Tag/Input/ColorTest.php @@ -0,0 +1,22 @@ +assertSame( + '', + Color::tag() + ->name('color') + ->value('#ff0000') + ->render() + ); + } +} diff --git a/tests/Tag/InputTest.php b/tests/Tag/InputTest.php index a6daebe8..78e29ea9 100644 --- a/tests/Tag/InputTest.php +++ b/tests/Tag/InputTest.php @@ -77,6 +77,14 @@ public function testRange(): void ); } + public function testColor(): void + { + $this->assertSame( + '', + (string) Input::color('color', '#ff0000') + ); + } + public function testButton(): void { $this->assertSame( From a573446ecc20d38d41116d7ddc8684a5fbd67ce1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C4=83zvan=20Biri=C8=99an?= Date: Sun, 7 Sep 2025 18:58:56 +0300 Subject: [PATCH 2/6] Fix style issues --- src/Html.php | 1 - src/Tag/Input/Color.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Html.php b/src/Html.php index dff7ad49..2e40313a 100644 --- a/src/Html.php +++ b/src/Html.php @@ -784,7 +784,6 @@ public static function textInput( return $attributes === [] ? $tag : $tag->addAttributes($attributes); } - /** * Generates a hidden input field. * diff --git a/src/Tag/Input/Color.php b/src/Tag/Input/Color.php index e75366b8..210ed9e8 100644 --- a/src/Tag/Input/Color.php +++ b/src/Tag/Input/Color.php @@ -8,7 +8,7 @@ final class Color extends InputTag { - final protected function prepareAttributes(): void + protected function prepareAttributes(): void { $this->attributes['type'] = $this->getType(); } From 3aa8b534592f685411d9d014c058686377659b88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C4=83zvan=20Biri=C8=99an?= Date: Mon, 8 Sep 2025 13:58:44 +0300 Subject: [PATCH 3/6] apply code review changes --- CHANGELOG.md | 1 + README.md | 2 +- src/Html.php | 2 +- src/Tag/Input/Color.php | 10 ++++------ tests/HtmlTest.php | 12 ++++++------ 5 files changed, 13 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bcde24c..13ba9024 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 3.11.1 under development - New #249: Add `Tag::addStyle()` and `Tag::removeStyle()` methods (@vjik) +- New #252: Add `Html::color()` and `Input::color()` methods (@razvbir) ## 3.11.0 June 10, 2025 diff --git a/README.md b/README.md index 2807dc03..fbc0f7d6 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ The package provides various tools to help with dynamic server-side generation o - Tag classes `A`, `Address`, `Article`, `Aside`, `Audio`, `B`, `Body`, `Br`, `Button`, `Caption`, `Col`, `Colgroup`, `Datalist`, `Div`, `Em`, `Fieldset`, `Footer`, `Form`, `H1`, `H2`, `H3`, `H4`, `H5`, `H6`, `Header`, `Hr`, `Hgroup`, - `Html`, `I`, `Img`, `Input` (and specialized `Checkbox`, `Radio`, `Range`, `File`), `Label`, `Legend`, `Li`, `Link`, + `Html`, `I`, `Img`, `Input` (and specialized `Checkbox`, `Radio`, `Range`, `File`, `Color`), `Label`, `Legend`, `Li`, `Link`, `Meta`, `Nav`, `Noscript`, `Ol`, `Optgroup`, `Option`, `P`, `Picture`, `Script`, `Section`, `Select`, `Small`, `Source`, `Span`, `Strong`, `Style`, `Table`, `Tbody`, `Td`, `Textarea`, `Tfoot`, `Th`, `Thead`, `Title`, `Tr`, `Track`, `Ul`, `Video`. diff --git a/src/Html.php b/src/Html.php index 2e40313a..6430df41 100644 --- a/src/Html.php +++ b/src/Html.php @@ -887,7 +887,7 @@ public static function range( * @param string|Stringable|null $value The value attribute. * @param array $attributes The tag attributes in terms of name-value pairs. */ - public static function colorInput( + public static function color( ?string $name = null, string|Stringable|null $value = null, array $attributes = [] diff --git a/src/Tag/Input/Color.php b/src/Tag/Input/Color.php index 210ed9e8..3d7ca78e 100644 --- a/src/Tag/Input/Color.php +++ b/src/Tag/Input/Color.php @@ -6,15 +6,13 @@ use Yiisoft\Html\Tag\Base\InputTag; +/** + * @link https://html.spec.whatwg.org/multipage/input.html#color-state-(type=color) + */ final class Color extends InputTag { protected function prepareAttributes(): void { - $this->attributes['type'] = $this->getType(); - } - - protected function getType(): string - { - return 'color'; + $this->attributes['type'] = 'color'; } } diff --git a/tests/HtmlTest.php b/tests/HtmlTest.php index eb3cdc38..4f374a81 100644 --- a/tests/HtmlTest.php +++ b/tests/HtmlTest.php @@ -384,17 +384,17 @@ public function testTextInput(): void public function testColorInput(): void { - $this->assertSame('', Html::colorInput()->render()); - $this->assertSame('', Html::colorInput('')->render()); - $this->assertSame('', Html::colorInput(null, '')->render()); - $this->assertSame('', Html::colorInput('test')->render()); + $this->assertSame('', Html::color()->render()); + $this->assertSame('', Html::color('')->render()); + $this->assertSame('', Html::color(null, '')->render()); + $this->assertSame('', Html::color('test')->render()); $this->assertSame( '', - Html::colorInput('test', '#ff0000')->render(), + Html::color('test', '#ff0000')->render(), ); $this->assertSame( '', - Html::colorInput('test', '#ff0000', ['required' => true])->render(), + Html::color('test', '#ff0000', ['required' => true])->render(), ); } From aac385ded42853dc5eb7f83af19a0352296dd532 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C4=83zvan=20Biri=C8=99an?= <6582802+razvbir@users.noreply.github.com> Date: Mon, 8 Sep 2025 16:57:20 +0300 Subject: [PATCH 4/6] Update CHANGELOG.md Co-authored-by: Sergei Predvoditelev --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13ba9024..b8e06cc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ## 3.11.1 under development - New #249: Add `Tag::addStyle()` and `Tag::removeStyle()` methods (@vjik) -- New #252: Add `Html::color()` and `Input::color()` methods (@razvbir) +- New #252: Add `Color` class, `Html::color()` and `Input::color()` methods (@razvbir) ## 3.11.0 June 10, 2025 From 8ac68f58b182944493138e0555273d6bdb264c35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C4=83zvan=20Biri=C8=99an?= Date: Mon, 8 Sep 2025 17:07:58 +0300 Subject: [PATCH 5/6] Add `color` to Form tags section --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index fbc0f7d6..b4a56bb4 100644 --- a/README.md +++ b/README.md @@ -332,6 +332,7 @@ Overall the helper has the following method groups. - option - passwordInput - radio +- color - resetButton - resetInput - select From c1e21c9ae09b00e41a6eb01d097f5901a4514008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C4=83zvan=20Biri=C8=99an?= Date: Mon, 8 Sep 2025 17:25:32 +0300 Subject: [PATCH 6/6] Sort Form tags list alphabetically --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b4a56bb4..2f68445c 100644 --- a/README.md +++ b/README.md @@ -319,9 +319,10 @@ Overall the helper has the following method groups. - button - buttonInput - checkbox -- file +- color - datalist - fieldset +- file - fileInput - form - hiddenInput @@ -332,14 +333,14 @@ Overall the helper has the following method groups. - option - passwordInput - radio -- color +- range - resetButton - resetInput - select - submitButton - submitInput -- textInput - textarea +- textInput #### Table tags