Skip to content

Commit

Permalink
Fix #113: Add class for tag Legend, class for tag Fieldset, metho…
Browse files Browse the repository at this point in the history
…ds `Html::legend()` and `Html::fieldset()`
  • Loading branch information
vjik committed Apr 15, 2022
1 parent d399fa0 commit b32a1cb
Show file tree
Hide file tree
Showing 8 changed files with 374 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
- New #103: Add class for tag `Form` and method `Html::form()` (vjik)
- New #109: Add class for tag `Datalist` and method `Html::datalist()` (vjik)
- New #109: Add specialized class for input tag with type `Range` and methods `Html::range()`, `Input::range()` (vjik)
- New #113: Add class for tag `Legend`, class for tag `Fieldset`, methods `Html::legend()` and `Html::fieldset()` (vjik)
- New #111: Add widget `ButtonGroup` (vjik)
- New #111: Add method `Tag::unionAttributes()` that available for all tags (vjik)
- Enh #106: Add option groups support to method `Select::optionsData()` (vjik)
Expand Down
11 changes: 7 additions & 4 deletions README.md
Expand Up @@ -17,10 +17,11 @@

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

- Tag classes `A`, `Audio`, `B`, `Br`, `Button`, `Caption`, `Col`, `Colgroup`, `Datalist`, `Div`, `Em`, `Form`, `H1`,
`H2`, `H3`, `H4`, `H5`, `H6` `I`, `Img`, `Input` (and specialized `Checkbox`, `Radio`, `Range`), `Label`, `Li`, `Link`,
`Meta`, `Noscript`, `Ol`, `Optgroup`, `Option`, `P`, `Picture`, `Script`, `Select`, `Source`, `Span`, `Strong`,
`Style`, `Table`, `Tbody`, `Td`, `Textarea`, `Tfoot`, `Th`, `Thead`, `Title`, `Tr`, `Track`, `Ul`, `Video`.
- Tag classes `A`, `Audio`, `B`, `Br`, `Button`, `Caption`, `Col`, `Colgroup`, `Datalist`, `Div`, `Em`, `Fieldset`,
`Form`, `H1`, `H2`, `H3`, `H4`, `H5`, `H6` `I`, `Img`, `Input` (and specialized `Checkbox`, `Radio`, `Range`), `Label`,
`Legend`, `Li`, `Link`, `Meta`, `Noscript`, `Ol`, `Optgroup`, `Option`, `P`, `Picture`, `Script`, `Select`, `Source`,
`Span`, `Strong`, `Style`, `Table`, `Tbody`, `Td`, `Textarea`, `Tfoot`, `Th`, `Thead`, `Title`, `Tr`, `Track`, `Ul`,
`Video`.
- `CustomTag` class that helps to generate custom tag with any attributes.
- HTML widgets `ButtonGroup`, `CheckboxList` and `RadioList`.
- All tags content is automatically HTML-encoded. There is `NoEncode` class designed to wrap content that should not be encoded.
Expand Down Expand Up @@ -301,11 +302,13 @@ Overall the helper has the following method groups.
- buttonInput
- checkbox
- datalist
- fieldset
- fileInput
- form
- hiddenInput
- input
- label
- legend
- optgroup
- option
- passwordInput
Expand Down
34 changes: 34 additions & 0 deletions src/Html.php
Expand Up @@ -19,6 +19,7 @@
use Yiisoft\Html\Tag\Datalist;
use Yiisoft\Html\Tag\Div;
use Yiisoft\Html\Tag\Em;
use Yiisoft\Html\Tag\Fieldset;
use Yiisoft\Html\Tag\Form;
use Yiisoft\Html\Tag\H1;
use Yiisoft\Html\Tag\H2;
Expand All @@ -33,6 +34,7 @@
use Yiisoft\Html\Tag\Input\Radio;
use Yiisoft\Html\Tag\Input\Range;
use Yiisoft\Html\Tag\Label;
use Yiisoft\Html\Tag\Legend;
use Yiisoft\Html\Tag\Li;
use Yiisoft\Html\Tag\Link;
use Yiisoft\Html\Tag\Audio;
Expand Down Expand Up @@ -533,6 +535,20 @@ public static function img(?string $url = null, ?string $alt = ''): Img
return $tag;
}

/**
* Generates a {@see Fieldset} tag.
*
* @param array $attributes The tag attributes in terms of name-value pairs.
*/
public static function fieldset(array $attributes = []): Fieldset
{
$tag = Fieldset::tag();
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $tag;
}

/**
* Generates a {@see Form} tag.
*
Expand Down Expand Up @@ -574,6 +590,24 @@ public static function label($content = '', ?string $for = null): Label
return $tag;
}

/**
* Generates a {@see Legend} tag.
*
* @param string|Stringable $content The tag content.
* @param array $attributes The tag attributes in terms of name-value pairs.
*/
public static function legend($content = '', array $attributes = []): Legend
{
$tag = Legend::tag();
if ($content !== '') {
$tag = $tag->content($content);
}
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $tag;
}

/**
* Generates a button tag.
*
Expand Down
80 changes: 80 additions & 0 deletions src/Tag/Fieldset.php
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Html\Tag;

use Stringable;
use Yiisoft\Html\Html;
use Yiisoft\Html\Tag\Base\NormalTag;
use Yiisoft\Html\Tag\Base\TagContentTrait;

final class Fieldset extends NormalTag
{
use TagContentTrait;

private ?Legend $legend = null;

/**
* @param string|Stringable|null $content
*/
public function legend($content, array $attributes = []): self
{
$new = clone $this;
$new->legend = $content === null ? null : Html::legend($content, $attributes);
return $new;
}

public function legendTag(?Legend $legend): self
{
$new = clone $this;
$new->legend = $legend;
return $new;
}

/**
* @link https://html.spec.whatwg.org/multipage/form-elements.html#attr-fieldset-disabled
*
* @param bool|null $disabled Whether fieldset is disabled.
*/
public function disabled(?bool $disabled = true): self
{
$new = clone $this;
$new->attributes['disabled'] = $disabled;
return $new;
}

/**
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form
*/
public function form(?string $formId): self
{
$new = clone $this;
$new->attributes['form'] = $formId;
return $new;
}

/**
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-name
*/
public function name(?string $name): self
{
$new = clone $this;
$new->attributes['name'] = $name;
return $new;
}

protected function prepend(): string
{
if ($this->legend === null) {
return '';
}

return "\n" . $this->legend->render() . "\n";
}

protected function getName(): string
{
return 'fieldset';
}
}
21 changes: 21 additions & 0 deletions src/Tag/Legend.php
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Html\Tag;

use Yiisoft\Html\Tag\Base\NormalTag;
use Yiisoft\Html\Tag\Base\TagContentTrait;

/**
* @link https://html.spec.whatwg.org/multipage/form-elements.html#the-legend-element
*/
final class Legend extends NormalTag
{
use TagContentTrait;

protected function getName(): string
{
return 'legend';
}
}
28 changes: 28 additions & 0 deletions tests/common/HtmlTest.php
Expand Up @@ -221,6 +221,18 @@ public function testImg(): void
$this->assertSame('<img src="face.png" alt="My Face">', Html::img('face.png', 'My Face')->render());
}

public function testFieldset(): void
{
$this->assertSame(
'<fieldset></fieldset>',
Html::fieldset()->render()
);
$this->assertSame(
'<fieldset id="MyFields"></fieldset>',
Html::fieldset(['id' => 'MyFields'])->render()
);
}

public function testForm(): void
{
$this->assertSame(
Expand Down Expand Up @@ -250,6 +262,22 @@ public function testLabel(): void
$this->assertSame('<label><span>Hello</span></label>', Html::label(Html::span('Hello'))->render());
}

public function testLegend(): void
{
$this->assertSame(
'<legend></legend>',
Html::legend()->render()
);
$this->assertSame(
'<legend>Your data</legend>',
Html::legend('Your data')->render()
);
$this->assertSame(
'<legend id="MyLegend">Your data</legend>',
Html::legend('Your data', ['id' => 'MyLegend'])->render()
);
}

public function testButton(): void
{
$this->assertSame(
Expand Down

0 comments on commit b32a1cb

Please sign in to comment.