Skip to content

Commit b32a1cb

Browse files
authored
Fix #113: Add class for tag Legend, class for tag Fieldset, methods Html::legend() and Html::fieldset()
1 parent d399fa0 commit b32a1cb

8 files changed

Lines changed: 374 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- New #103: Add class for tag `Form` and method `Html::form()` (vjik)
66
- New #109: Add class for tag `Datalist` and method `Html::datalist()` (vjik)
77
- New #109: Add specialized class for input tag with type `Range` and methods `Html::range()`, `Input::range()` (vjik)
8+
- New #113: Add class for tag `Legend`, class for tag `Fieldset`, methods `Html::legend()` and `Html::fieldset()` (vjik)
89
- New #111: Add widget `ButtonGroup` (vjik)
910
- New #111: Add method `Tag::unionAttributes()` that available for all tags (vjik)
1011
- Enh #106: Add option groups support to method `Select::optionsData()` (vjik)

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717

1818
The package provides various tools to help with dynamic server-side generation of HTML:
1919

20-
- Tag classes `A`, `Audio`, `B`, `Br`, `Button`, `Caption`, `Col`, `Colgroup`, `Datalist`, `Div`, `Em`, `Form`, `H1`,
21-
`H2`, `H3`, `H4`, `H5`, `H6` `I`, `Img`, `Input` (and specialized `Checkbox`, `Radio`, `Range`), `Label`, `Li`, `Link`,
22-
`Meta`, `Noscript`, `Ol`, `Optgroup`, `Option`, `P`, `Picture`, `Script`, `Select`, `Source`, `Span`, `Strong`,
23-
`Style`, `Table`, `Tbody`, `Td`, `Textarea`, `Tfoot`, `Th`, `Thead`, `Title`, `Tr`, `Track`, `Ul`, `Video`.
20+
- Tag classes `A`, `Audio`, `B`, `Br`, `Button`, `Caption`, `Col`, `Colgroup`, `Datalist`, `Div`, `Em`, `Fieldset`,
21+
`Form`, `H1`, `H2`, `H3`, `H4`, `H5`, `H6` `I`, `Img`, `Input` (and specialized `Checkbox`, `Radio`, `Range`), `Label`,
22+
`Legend`, `Li`, `Link`, `Meta`, `Noscript`, `Ol`, `Optgroup`, `Option`, `P`, `Picture`, `Script`, `Select`, `Source`,
23+
`Span`, `Strong`, `Style`, `Table`, `Tbody`, `Td`, `Textarea`, `Tfoot`, `Th`, `Thead`, `Title`, `Tr`, `Track`, `Ul`,
24+
`Video`.
2425
- `CustomTag` class that helps to generate custom tag with any attributes.
2526
- HTML widgets `ButtonGroup`, `CheckboxList` and `RadioList`.
2627
- All tags content is automatically HTML-encoded. There is `NoEncode` class designed to wrap content that should not be encoded.
@@ -301,11 +302,13 @@ Overall the helper has the following method groups.
301302
- buttonInput
302303
- checkbox
303304
- datalist
305+
- fieldset
304306
- fileInput
305307
- form
306308
- hiddenInput
307309
- input
308310
- label
311+
- legend
309312
- optgroup
310313
- option
311314
- passwordInput

src/Html.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Yiisoft\Html\Tag\Datalist;
2020
use Yiisoft\Html\Tag\Div;
2121
use Yiisoft\Html\Tag\Em;
22+
use Yiisoft\Html\Tag\Fieldset;
2223
use Yiisoft\Html\Tag\Form;
2324
use Yiisoft\Html\Tag\H1;
2425
use Yiisoft\Html\Tag\H2;
@@ -33,6 +34,7 @@
3334
use Yiisoft\Html\Tag\Input\Radio;
3435
use Yiisoft\Html\Tag\Input\Range;
3536
use Yiisoft\Html\Tag\Label;
37+
use Yiisoft\Html\Tag\Legend;
3638
use Yiisoft\Html\Tag\Li;
3739
use Yiisoft\Html\Tag\Link;
3840
use Yiisoft\Html\Tag\Audio;
@@ -533,6 +535,20 @@ public static function img(?string $url = null, ?string $alt = ''): Img
533535
return $tag;
534536
}
535537

538+
/**
539+
* Generates a {@see Fieldset} tag.
540+
*
541+
* @param array $attributes The tag attributes in terms of name-value pairs.
542+
*/
543+
public static function fieldset(array $attributes = []): Fieldset
544+
{
545+
$tag = Fieldset::tag();
546+
if (!empty($attributes)) {
547+
$tag = $tag->attributes($attributes);
548+
}
549+
return $tag;
550+
}
551+
536552
/**
537553
* Generates a {@see Form} tag.
538554
*
@@ -574,6 +590,24 @@ public static function label($content = '', ?string $for = null): Label
574590
return $tag;
575591
}
576592

593+
/**
594+
* Generates a {@see Legend} tag.
595+
*
596+
* @param string|Stringable $content The tag content.
597+
* @param array $attributes The tag attributes in terms of name-value pairs.
598+
*/
599+
public static function legend($content = '', array $attributes = []): Legend
600+
{
601+
$tag = Legend::tag();
602+
if ($content !== '') {
603+
$tag = $tag->content($content);
604+
}
605+
if (!empty($attributes)) {
606+
$tag = $tag->attributes($attributes);
607+
}
608+
return $tag;
609+
}
610+
577611
/**
578612
* Generates a button tag.
579613
*

src/Tag/Fieldset.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Html\Tag;
6+
7+
use Stringable;
8+
use Yiisoft\Html\Html;
9+
use Yiisoft\Html\Tag\Base\NormalTag;
10+
use Yiisoft\Html\Tag\Base\TagContentTrait;
11+
12+
final class Fieldset extends NormalTag
13+
{
14+
use TagContentTrait;
15+
16+
private ?Legend $legend = null;
17+
18+
/**
19+
* @param string|Stringable|null $content
20+
*/
21+
public function legend($content, array $attributes = []): self
22+
{
23+
$new = clone $this;
24+
$new->legend = $content === null ? null : Html::legend($content, $attributes);
25+
return $new;
26+
}
27+
28+
public function legendTag(?Legend $legend): self
29+
{
30+
$new = clone $this;
31+
$new->legend = $legend;
32+
return $new;
33+
}
34+
35+
/**
36+
* @link https://html.spec.whatwg.org/multipage/form-elements.html#attr-fieldset-disabled
37+
*
38+
* @param bool|null $disabled Whether fieldset is disabled.
39+
*/
40+
public function disabled(?bool $disabled = true): self
41+
{
42+
$new = clone $this;
43+
$new->attributes['disabled'] = $disabled;
44+
return $new;
45+
}
46+
47+
/**
48+
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form
49+
*/
50+
public function form(?string $formId): self
51+
{
52+
$new = clone $this;
53+
$new->attributes['form'] = $formId;
54+
return $new;
55+
}
56+
57+
/**
58+
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-name
59+
*/
60+
public function name(?string $name): self
61+
{
62+
$new = clone $this;
63+
$new->attributes['name'] = $name;
64+
return $new;
65+
}
66+
67+
protected function prepend(): string
68+
{
69+
if ($this->legend === null) {
70+
return '';
71+
}
72+
73+
return "\n" . $this->legend->render() . "\n";
74+
}
75+
76+
protected function getName(): string
77+
{
78+
return 'fieldset';
79+
}
80+
}

src/Tag/Legend.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Html\Tag;
6+
7+
use Yiisoft\Html\Tag\Base\NormalTag;
8+
use Yiisoft\Html\Tag\Base\TagContentTrait;
9+
10+
/**
11+
* @link https://html.spec.whatwg.org/multipage/form-elements.html#the-legend-element
12+
*/
13+
final class Legend extends NormalTag
14+
{
15+
use TagContentTrait;
16+
17+
protected function getName(): string
18+
{
19+
return 'legend';
20+
}
21+
}

tests/common/HtmlTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,18 @@ public function testImg(): void
221221
$this->assertSame('<img src="face.png" alt="My Face">', Html::img('face.png', 'My Face')->render());
222222
}
223223

224+
public function testFieldset(): void
225+
{
226+
$this->assertSame(
227+
'<fieldset></fieldset>',
228+
Html::fieldset()->render()
229+
);
230+
$this->assertSame(
231+
'<fieldset id="MyFields"></fieldset>',
232+
Html::fieldset(['id' => 'MyFields'])->render()
233+
);
234+
}
235+
224236
public function testForm(): void
225237
{
226238
$this->assertSame(
@@ -250,6 +262,22 @@ public function testLabel(): void
250262
$this->assertSame('<label><span>Hello</span></label>', Html::label(Html::span('Hello'))->render());
251263
}
252264

265+
public function testLegend(): void
266+
{
267+
$this->assertSame(
268+
'<legend></legend>',
269+
Html::legend()->render()
270+
);
271+
$this->assertSame(
272+
'<legend>Your data</legend>',
273+
Html::legend('Your data')->render()
274+
);
275+
$this->assertSame(
276+
'<legend id="MyLegend">Your data</legend>',
277+
Html::legend('Your data', ['id' => 'MyLegend'])->render()
278+
);
279+
}
280+
253281
public function testButton(): void
254282
{
255283
$this->assertSame(

0 commit comments

Comments
 (0)