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 @@ -6,6 +6,7 @@
- Enh #261: Enhance `RadioList::addRadioWrapClass()` method for cleaner class addition (@vjik)
- Enh #263: Explicitly import classes and constants in "use" section (@mspirkov)
- Chg #243: Deprecate static constructors such as `TagName::tag()` in favor of `new TagName()` (@razvbir)
- Chg #221: Throw `LogicException` in `Tag::id()` when id is empty string (@razvbir)

## 3.12.0 December 13, 2025

Expand Down
7 changes: 6 additions & 1 deletion src/Tag/Base/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Html\Tag\Base;

use BackedEnum;
use LogicException;
use Yiisoft\Html\Html;
use Yiisoft\Html\NoEncodeStringableInterface;

Expand Down Expand Up @@ -75,10 +76,14 @@ final public function attribute(string $name, mixed $value): static
*
* @param string|null $id Tag ID.
*
* @psalm-param non-empty-string|null $id
* @throws LogicException
*/
final public function id(?string $id): static
{
if ($id === '') {
throw new LogicException('The tag id cannot be an empty string.');
}

$new = clone $this;
$new->attributes['id'] = $id;
return $new;
Expand Down
8 changes: 8 additions & 0 deletions tests/Tag/Base/TagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Html\Tests\Tag\Base;

use LogicException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Yiisoft\Html\Tests\Objects\TestTag;
Expand Down Expand Up @@ -132,6 +133,13 @@ public function testId(string $expected, ?string $id): void
$this->assertSame($expected, (string) (new TestTag())->id($id));
}

public function testIdEmptyString(): void
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('The tag id cannot be an empty string.');
(new TestTag())->id('');
}

public static function dataAddClass(): array
{
return [
Expand Down
Loading