Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Chg #234: Remove tag attributes sorting (@FrankiFixx)
- Chg #269: Rename `$options` parameter to `$attributes` in `Html::addCssClass()`, `Html::removeCssClass()`,
`Html::addCssStyle()` and `Html::removeCssStyle()` methods (@vjik)
- New #270: Allow passing `int`, `float` and `null` as tag content (@vjik)

## 3.13.0 March 13, 2026

Expand Down
176 changes: 88 additions & 88 deletions src/Html.php

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions src/Tag/Base/ListTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Html\Tag\Base;

use Stringable;
use Yiisoft\Html\Tag\Li;

/**
Expand All @@ -27,14 +28,14 @@ public function items(Li ...$items): static
}

/**
* @param string[] $strings Array of list items as strings.
* @param (string|Stringable|int|float|null)[] $strings Array of list item contents.
* @param array $attributes The tag attributes in terms of name-value pairs.
* @param bool $encode Whether to encode strings passed.
* @param bool $encode Whether to encode the item contents when rendering.
*/
public function strings(array $strings, array $attributes = [], bool $encode = true): static
{
$items = array_map(
static fn(string $string) => (new Li())
static fn(string|Stringable|int|float|null $string) => (new Li())
->content($string)
->attributes($attributes)
->encode($encode),
Expand Down
12 changes: 6 additions & 6 deletions src/Tag/Base/TagContentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
private bool $doubleEncode = true;

/**
* @psalm-var list<string|Stringable>
* @psalm-var list<string|Stringable|int|float|null>
*/
private array $content = [];

Expand Down Expand Up @@ -48,19 +48,19 @@
}

/**
* @param string|Stringable ...$content Tag content.
* @param string|Stringable|int|float|null ...$content Tag content.
*/
final public function content(string|Stringable ...$content): static
final public function content(string|Stringable|int|float|null ...$content): static
{
$new = clone $this;
$new->content = array_values($content);
return $new;
}

/**
* @param string|Stringable ...$content Tag content.
* @param string|Stringable|int|float|null ...$content Tag content.
*/
final public function addContent(string|Stringable ...$content): static
final public function addContent(string|Stringable|int|float|null ...$content): static
{
$new = clone $this;
$new->content = array_merge($new->content, array_values($content));
Expand All @@ -79,7 +79,7 @@
$item = Html::encode($item, $this->doubleEncode);
}

$content .= $item;
$content .= (string) $item;

Check warning on line 82 in src/Tag/Base/TagContentTrait.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "CastString": @@ @@ $item = Html::encode($item, $this->doubleEncode); } - $content .= (string) $item; + $content .= $item; } return $content;
}

return $content;
Expand Down
7 changes: 4 additions & 3 deletions src/Tag/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Html\Tag;

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

Expand All @@ -14,21 +15,21 @@ final class Button extends NormalTag
{
use TagContentTrait;

public static function button(string $content = ''): self
public static function button(string|Stringable|int|float|null $content = ''): self
{
$button = (new self())->content($content);
$button->attributes['type'] = 'button';
return $button;
}

public static function submit(string $content = ''): self
public static function submit(string|Stringable|int|float|null $content = ''): self
{
$button = (new self())->content($content);
$button->attributes['type'] = 'submit';
return $button;
}

public static function reset(string $content = ''): self
public static function reset(string|Stringable|int|float|null $content = ''): self
{
$button = (new self())->content($content);
$button->attributes['type'] = 'reset';
Expand Down
2 changes: 1 addition & 1 deletion src/Tag/Optgroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function options(Option ...$options): self
/**
* Options as a set of value-content pairs.
*
* @param string[] $data Value-content set of options.
* @param (string|Stringable|int|float|null)[] $data Value-content set of options.
* @param bool $encode Whether to encode option content.
* @param array[] $optionsAttributes Array of option attribute sets indexed by option values from {@see $data}.
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Tag/Script.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ final class Script extends NormalTag
/**
* @link https://www.w3.org/TR/html52/semantics-scripting.html#script-content-restrictions
*
* @param string $content Tag content.
* @param string|Stringable $content Tag content.
*/
public function content(string $content): self
public function content(string|Stringable $content): self
{
$new = clone $this;
$new->content = $content;
$new->content = (string) $content;
return $new;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Tag/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
* @link https://www.w3.org/TR/html52/sec-forms.html#the-select-element
*
* @psalm-type OptionsData = array<array-key, string|array<array-key,string>>
* @psalm-type OptionsData = array<array-key, string|Stringable|int|float|null|array<array-key,string|Stringable|int|float|null>>
*/
final class Select extends NormalTag
{
Expand Down Expand Up @@ -71,7 +71,7 @@
*/
public function values(iterable $values): self
{
$values = is_array($values) ? $values : iterator_to_array($values);

Check warning on line 74 in src/Tag/Select.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "Ternary": @@ @@ */ public function values(iterable $values): self { - $values = is_array($values) ? $values : iterator_to_array($values); + $values = is_array($values) ? iterator_to_array($values) : $values; return $this->value(...$values); }

return $this->value(...$values);
}
Expand Down Expand Up @@ -168,10 +168,10 @@
}

/**
* @param string|null $text Text of the option that has dummy value and is rendered
* @param string|Stringable|int|float|null $text Text of the option that has dummy value and is rendered
* as an invitation to select a value.
*/
public function prompt(?string $text): self
public function prompt(string|Stringable|int|float|null $text): self
{
$new = clone $this;
$new->prompt = $text === null ? null : (new Option())
Expand Down Expand Up @@ -273,7 +273,7 @@

protected function before(): string
{
$name = (string) ($this->attributes['name'] ?? '');

Check warning on line 276 in src/Tag/Select.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "CastString": @@ @@ protected function before(): string { - $name = (string) ($this->attributes['name'] ?? ''); + $name = $this->attributes['name'] ?? ''; if ( empty($name) || (
if (
empty($name)
|| (
Expand Down
7 changes: 4 additions & 3 deletions src/Tag/Style.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Html\Tag;

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

/**
Expand All @@ -14,12 +15,12 @@ final class Style extends NormalTag
private string $content = '';

/**
* @param string $content Tag content.
* @param string|Stringable $content Tag content.
*/
public function content(string $content): self
public function content(string|Stringable $content): self
{
$new = clone $this;
$new->content = $content;
$new->content = (string) $content;
return $new;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Tag/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Html\Tag;

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

/**
Expand Down Expand Up @@ -44,7 +45,7 @@ public function caption(?Caption $caption): self
return $new;
}

public function captionString(string $content, bool $encode = true): self
public function captionString(string|Stringable|int|float|null $content, bool $encode = true): self
{
$caption = (new Caption())->content($content);
if (!$encode) {
Expand Down
4 changes: 2 additions & 2 deletions src/Tag/Textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public function columns(?int $count): self
}

/**
* @param string|string[]|Stringable|null $value
* @param string|string[]|Stringable|int|float|null $value
*/
public function value(string|Stringable|array|null $value): self
public function value(string|Stringable|int|float|array|null $value): self
{
$content = is_array($value)
? implode("\n", $value)
Expand Down
13 changes: 7 additions & 6 deletions src/Tag/Tr.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Html\Tag;

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

Expand Down Expand Up @@ -58,7 +59,7 @@ public function addDataStrings(array $strings, array $attributes = [], bool $enc
}

/**
* @param string[] $strings Array of header cells ({@see Th}) as strings.
* @param (string|Stringable|int|float|null)[] $strings Array of header cells ({@see Th}) contents.
* @param array $attributes The tag attributes in terms of name-value pairs.
* @param bool $encode Whether to encode strings passed.
*/
Expand All @@ -68,7 +69,7 @@ public function headerStrings(array $strings, array $attributes = [], bool $enco
}

/**
* @param string[] $strings Array of header cells ({@see Th}) as strings.
* @param (string|Stringable|int|float|null)[] $strings Array of header cells ({@see Th}) contents.
* @param array $attributes The tag attributes in terms of name-value pairs.
* @param bool $encode Whether to encode strings passed.
*/
Expand All @@ -90,14 +91,14 @@ protected function getName(): string
}

/**
* @param string[] $strings
* @param (string|Stringable|int|float|null)[] $strings
*
* @return Td[]
*/
private function makeDataCells(array $strings, array $attributes, bool $encode): array
{
return array_map(
static fn(string $string) => (new Td())
static fn(string|Stringable|int|float|null $string) => (new Td())
->content($string)
->attributes($attributes)
->encode($encode),
Expand All @@ -106,14 +107,14 @@ private function makeDataCells(array $strings, array $attributes, bool $encode):
}

/**
* @param string[] $strings
* @param (string|Stringable|int|float|null)[] $strings
*
* @return Th[]
*/
private function makeHeaderCells(array $strings, array $attributes, bool $encode): array
{
return array_map(
static fn(string $string) => (new Th())
static fn(string|Stringable|int|float|null $string) => (new Th())
->content($string)
->attributes($attributes)
->encode($encode),
Expand Down
Loading
Loading