Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow pass null argument to methods Tag::class(), Tag::replaceClass(), BooleanInputTag::label() and BooleanInputTag::sideLabel() #78

Merged
merged 9 commits into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Yii HTML Change Log

## 1.2.1 under development
## 1.3.0 under development

- New #74: Add classes for tags `Em`, `Strong`, `B` and `I` (vjik)
- New #75: Add methods `as()` and `preload()` to the `Link` tag (vjik)
- New #78: Allow pass `null` argument to methods `Tag::class()`, `Tag::replaceClass()`, `BooleanInputTag::label()` and
`BooleanInputTag::sideLabel()` (vjik)

## 1.2.0 May 04, 2021

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
"yiisoft/json": "^1.0"
},
"require-dev": {
"maglnet/composer-require-checker": "^3.2",
"maglnet/composer-require-checker": "^3.3",
"phpunit/phpunit": "^9.5",
"roave/infection-static-analysis-plugin": "^1.7",
"roave/infection-static-analysis-plugin": "^1.9",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.7"
"vimeo/psalm": "^4.8"
},
"autoload": {
"psr-4": {
Expand Down
5 changes: 4 additions & 1 deletion src/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,10 @@ public static function renderTagAttributes(array $attributes): string
if (empty($value)) {
continue;
}
/** @psalm-var array<string, string> $value */
/**
* @psalm-suppress UnnecessaryVarAnnotation
* @psalm-var array<string, string> $value
*/
$html .= " $name=\"" . self::encodeAttribute(self::cssStyleFromArray($value)) . '"';
} else {
$html .= " $name='" . Json::htmlEncode($value) . "'";
Expand Down
20 changes: 11 additions & 9 deletions src/Tag/Base/BooleanInputTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ final public function checked(bool $checked = true): self
/**
* Label that is wraps around attribute when rendered.
*
* @param string $label Input label.
* @param string|null $label Input label.
* @param array $attributes Name-value set of label attributes.
*
* @return static
*/
final public function label(string $label, array $attributes = []): self
final public function label(?string $label, array $attributes = []): self
{
$new = clone $this;
$new->label = $label;
Expand All @@ -51,12 +51,12 @@ final public function label(string $label, array $attributes = []): self
/**
* Label that is rendered separately and is referring input by ID.
*
* @param string $label Input label.
* @param string|null $label Input label.
* @param array $attributes Name-value set of label attributes.
*
* @return static
*/
final public function sideLabel(string $label, array $attributes = []): self
final public function sideLabel(?string $label, array $attributes = []): self
{
$new = clone $this;
$new->label = $label;
Expand Down Expand Up @@ -96,7 +96,9 @@ final protected function prepareAttributes(): void

final protected function before(): string
{
$this->attributes['id'] ??= $this->labelWrap ? null : Html::generateId();
$this->attributes['id'] ??= ($this->labelWrap || $this->label === null)
? null
: Html::generateId();

return $this->renderUncheckInput() .
($this->labelWrap ? $this->renderLabelOpenTag($this->labelAttributes) : '');
Expand Down Expand Up @@ -141,13 +143,13 @@ final protected function after(): string
return '';
}

$html = ' ';

if (!$this->labelWrap) {
if ($this->labelWrap) {
$html = $this->label === '' ? '' : ' ';
} else {
$labelAttributes = array_merge($this->labelAttributes, [
'for' => $this->attributes['id'],
]);
$html .= $this->renderLabelOpenTag($labelAttributes);
$html = ' ' . $this->renderLabelOpenTag($labelAttributes);
}

$html .= $this->labelEncode ? Html::encode($this->label) : $this->label;
Expand Down
15 changes: 9 additions & 6 deletions src/Tag/Base/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,32 @@ final public function id(?string $id): self
/**
* Add one or more CSS classes to the tag.
*
* @param string ...$class One or many CSS classes.
* @param string|null ...$class One or many CSS classes.
*
* @return static
*/
final public function class(string ...$class): self
final public function class(?string ...$class): self
{
$new = clone $this;
/** @psalm-suppress MixedArgumentTypeCoercion */
Html::addCssClass($new->attributes, $class);
Html::addCssClass(
$new->attributes,
array_filter($class, static fn ($c) => $c !== null),
);
return $new;
}

/**
* Replace current tag CSS classes with a new set of classes.
*
* @param string ...$class One or many CSS classes.
* @param string|null ...$class One or many CSS classes.
*
* @return static
*/
final public function replaceClass(string ...$class): self
final public function replaceClass(?string ...$class): self
{
$new = clone $this;
$new->attributes['class'] = $class;
$new->attributes['class'] = array_filter($class, static fn ($c) => $c !== null);
return $new;
}

Expand Down
28 changes: 27 additions & 1 deletion tests/common/Tag/Base/BooleanInputTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,23 @@ public function dataLabel(): array
'One',
['class' => 'red'],
],
[
'<label><input type="test"></label>',
'',
[],
],
[
'<input type="test">',
null,
[],
],
];
}

/**
* @dataProvider dataLabel
*/
public function testLabel(string $expected, string $label, array $attributes): void
public function testLabel(string $expected, ?string $label, array $attributes): void
{
$this->assertSame(
$expected,
Expand All @@ -64,6 +74,22 @@ public function testSideLabel(): void
);
}

public function testSideLabelEmpty(): void
{
$this->assertMatchesRegularExpression(
'~<input type="test" id="i(\d*?)"> <label for="i\1"></label>~',
TestBooleanInputTag::tag()->sideLabel('')->render()
);
}

public function testSideLabelNull(): void
{
$this->assertSame(
'<input type="test">',
TestBooleanInputTag::tag()->sideLabel(null)->render()
);
}

public function testSideLabelWithId(): void
{
$this->assertSame(
Expand Down
23 changes: 22 additions & 1 deletion tests/common/Tag/Base/TagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public function dataAttributes(): array
'<test checked disabled required="yes">',
['checked' => true, 'disabled' => true, 'hidden' => false, 'required' => 'yes'],
],
['<test class="">', ['class' => '']],
['<test class="red">', ['class' => 'red']],
['<test class="first second">', ['class' => ['first', 'second']]],
['<test>', ['class' => []]],
['<test style="width: 100px; height: 200px;">', ['style' => ['width' => '100px', 'height' => '200px']]],
Expand Down Expand Up @@ -141,10 +143,29 @@ public function testClass(string $expected, array $class): void
$this->assertSame($expected, (string)TestTag::tag()->class('main')->class(...$class));
}

public function dataNewClass(): array
{
return [
['<test>', null],
['<test class="">', ''],
['<test class="red">', 'red'],
];
}

/**
* @dataProvider dataNewClass
*/
public function testNewClass(string $expected, ?string $class): void
{
$this->assertSame($expected, (string)TestTag::tag()->class($class));
}

public function dataReplaceClass(): array
{
return [
['<test>', []],
['<test>', [null]],
['<test class="">', ['']],
['<test class="main">', ['main']],
['<test class="main bold">', ['main bold']],
['<test class="main bold">', ['main', 'bold']],
Expand All @@ -158,7 +179,7 @@ public function dataReplaceClass(): array
*/
public function testReplaceClass(string $expected, array $class): void
{
$this->assertSame($expected, (string)TestTag::tag()->replaceClass(...$class));
$this->assertSame($expected, (string)TestTag::tag()->class('red')->replaceClass(...$class));
}

public function testImmutability(): void
Expand Down