Skip to content

Commit e38d7ca

Browse files
authored
Add Tag::addClass() and deprecate Tag::class() (#130)
1 parent e62c412 commit e38d7ca

25 files changed

Lines changed: 51 additions & 34 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 2.4.1 under development
44

5+
- New #123: Add `Tag::addClass()` and deprecate `Tag::class()` (@vjik)
56
- New #129: Add methods `enctypeApplicationXWwwFormUrlencoded()`, `enctypeMultipartFormData()` and `enctypeTextPlain()`
67
to `Form` tag class (@vjik)
78

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ use Yiisoft\Html\Tag\Meta;
6565
<?= Html::openTag('div', ['class' => 'container flex-fill']) ?>
6666
<?= Html::p('', ['class' => 'float-left']) ?>
6767
<?= Html::p()
68-
->class('float-right')
68+
->replaceClass('float-right')
6969
->content(
7070
'Powered by ',
7171
Html::a(
@@ -93,7 +93,7 @@ echo \Yiisoft\Html\Tag\Div::tag()
9393
)
9494
->encode(false)
9595
->id('ContactEmail')
96-
->class('red');
96+
->replaceClass('red');
9797
```
9898

9999
... will generate the following HTML:

src/Tag/Base/Tag.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,23 @@ final public function id(?string $id): self
9292
* @param string|null ...$class One or many CSS classes.
9393
*
9494
* @return static
95+
*
96+
* @deprecated Use {@see addClass()} or {@see replaceClass()} instead. In the next major version `replaceClass()`
97+
* method will be renamed to `class()`.
9598
*/
9699
final public function class(?string ...$class): self
100+
{
101+
return $this->addClass(...$class);
102+
}
103+
104+
/**
105+
* Add one or more CSS classes to the tag.
106+
*
107+
* @param string|null ...$class One or many CSS classes.
108+
*
109+
* @return static
110+
*/
111+
final public function addClass(?string ...$class): self
97112
{
98113
$new = clone $this;
99114
Html::addCssClass(

tests/common/Tag/BTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function testBase(): void
1414
$this->assertSame(
1515
'<b class="red">Hello</b>',
1616
(string)B::tag()
17-
->class('red')
17+
->replaceClass('red')
1818
->content('Hello')
1919
);
2020
}

tests/common/Tag/Base/TagTest.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function testAttributesMerge(): void
7171
'<test id="color" class="green">',
7272
TestTag::tag()
7373
->id('color')
74-
->class('red')
74+
->replaceClass('red')
7575
->attributes(['class' => 'green'])
7676
->render(),
7777
);
@@ -83,7 +83,7 @@ public function testReplaceAttributes(): void
8383
'<test class="green">',
8484
TestTag::tag()
8585
->id('color')
86-
->class('red')
86+
->replaceClass('red')
8787
->replaceAttributes(['class' => 'green'])
8888
->render(),
8989
);
@@ -94,7 +94,7 @@ public function testUnionAttributes(): void
9494
$this->assertSame(
9595
'<test id="color" class="red">',
9696
TestTag::tag()
97-
->class('red')
97+
->replaceClass('red')
9898
->unionAttributes(['class' => 'green', 'id' => 'color'])
9999
->render(),
100100
);
@@ -151,11 +151,11 @@ public function dataClass(): array
151151
*
152152
* @param string[] $class
153153
*/
154-
public function testClass(string $expected, array $class): void
154+
public function testAddClass(string $expected, array $class): void
155155
{
156156
$this->assertSame($expected, (string)TestTag::tag()
157-
->class('main')
158-
->class(...$class));
157+
->addClass('main')
158+
->addClass(...$class));
159159
}
160160

161161
public function dataNewClass(): array
@@ -172,7 +172,7 @@ public function dataNewClass(): array
172172
*/
173173
public function testNewClass(string $expected, ?string $class): void
174174
{
175-
$this->assertSame($expected, (string)TestTag::tag()->class($class));
175+
$this->assertSame($expected, (string)TestTag::tag()->addClass($class));
176176
}
177177

178178
public function dataReplaceClass(): array
@@ -195,7 +195,7 @@ public function dataReplaceClass(): array
195195
public function testReplaceClass(string $expected, array $class): void
196196
{
197197
$this->assertSame($expected, (string)TestTag::tag()
198-
->class('red')
198+
->replaceClass('red')
199199
->replaceClass(...$class));
200200
}
201201

@@ -208,6 +208,7 @@ public function testImmutability(): void
208208
$this->assertNotSame($tag, $tag->attribute('id', null));
209209
$this->assertNotSame($tag, $tag->id(null));
210210
$this->assertNotSame($tag, $tag->class('test'));
211+
$this->assertNotSame($tag, $tag->addClass('test'));
211212
$this->assertNotSame($tag, $tag->replaceClass('test'));
212213
}
213214
}

tests/common/Tag/ColgroupTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ public function testBase(): void
2323
Col::tag(),
2424
Col::tag()
2525
->span(2)
26-
->class('red'),
26+
->replaceClass('red'),
2727
Col::tag()
2828
->span(2)
29-
->class('blue'),
29+
->replaceClass('blue'),
3030
)
3131
->render()
3232
);

tests/common/Tag/DivTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function testBase(): void
1414
$this->assertSame(
1515
'<div class="red">Hello</div>',
1616
(string)Div::tag()
17-
->class('red')
17+
->replaceClass('red')
1818
->content('Hello')
1919
);
2020
}

tests/common/Tag/EmTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function testBase(): void
1414
$this->assertSame(
1515
'<em class="red">Hello</em>',
1616
(string)Em::tag()
17-
->class('red')
17+
->replaceClass('red')
1818
->content('Hello')
1919
);
2020
}

tests/common/Tag/H1Test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function testBase(): void
1414
$this->assertSame(
1515
'<h1 class="red">Hello</h1>',
1616
(string) H1::tag()
17-
->class('red')
17+
->replaceClass('red')
1818
->content('Hello')
1919
);
2020
}

tests/common/Tag/H2Test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function testBase(): void
1414
$this->assertSame(
1515
'<h2 class="red">Hello</h2>',
1616
(string) H2::tag()
17-
->class('red')
17+
->replaceClass('red')
1818
->content('Hello')
1919
);
2020
}

0 commit comments

Comments
 (0)