44
55namespace Yiisoft \Html \Tag ;
66
7- use Stringable ;
8- use Yiisoft \Html \Html ;
9- use Yiisoft \Html \NoEncodeStringableInterface ;
107use Yiisoft \Html \Tag \Base \Tag ;
8+ use Yiisoft \Html \Tag \Base \TagContentTrait ;
119
1210/**
1311 * Custom HTML tag.
1412 */
1513final class CustomTag extends Tag
1614{
17- private const TYPE_AUTO = 0 ;
18- private const TYPE_NORMAL = 1 ;
19- private const TYPE_VOID = 2 ;
20-
21- private int $ type = self ::TYPE_AUTO ;
15+ use TagContentTrait;
2216
2317 /**
2418 * List of void elements. These only have a start tag; end tags must not be specified.
@@ -44,14 +38,12 @@ final class CustomTag extends Tag
4438 'wbr ' => 1 ,
4539 ];
4640
47- private string $ name ;
48- private ? bool $ encode = null ;
49- private bool $ doubleEncode = true ;
41+ private const TYPE_AUTO = 0 ;
42+ private const TYPE_NORMAL = 1 ;
43+ private const TYPE_VOID = 2 ;
5044
51- /**
52- * @var string[]|Stringable[]
53- */
54- private array $ content = [];
45+ private int $ type = self ::TYPE_AUTO ;
46+ private string $ name ;
5547
5648 private function __construct (string $ name )
5749 {
@@ -98,60 +90,6 @@ public function void(): self
9890 return $ new ;
9991 }
10092
101- /**
102- * @param bool|null $encode Whether to encode tag content. Supported values:
103- * - `null`: stringable objects that implement interface {@see NoEncodeStringableInterface} are not encoded,
104- * everything else is encoded;
105- * - `true`: any content is encoded;
106- * - `false`: nothing is encoded.
107- * Defaults to `null`.
108- *
109- * @return static
110- */
111- public function encode (?bool $ encode ): self
112- {
113- $ new = clone $ this ;
114- $ new ->encode = $ encode ;
115- return $ new ;
116- }
117-
118- /**
119- * @param bool $doubleEncode Whether already encoded HTML entities in tag content should be encoded.
120- * Defaults to `true`.
121- *
122- * @return static
123- */
124- public function doubleEncode (bool $ doubleEncode ): self
125- {
126- $ new = clone $ this ;
127- $ new ->doubleEncode = $ doubleEncode ;
128- return $ new ;
129- }
130-
131- /**
132- * @param string|Stringable ...$content Tag content.
133- *
134- * @return static
135- */
136- public function content (...$ content ): self
137- {
138- $ new = clone $ this ;
139- $ new ->content = $ content ;
140- return $ new ;
141- }
142-
143- /**
144- * @param string|Stringable ...$content Tag content.
145- *
146- * @return static
147- */
148- public function addContent (...$ content ): self
149- {
150- $ new = clone $ this ;
151- $ new ->content = [...$ new ->content , ...$ content ];
152- return $ new ;
153- }
154-
15593 protected function getName (): string
15694 {
15795 return $ this ->name ;
@@ -179,21 +117,4 @@ public function close(): string
179117 {
180118 return '</ ' . $ this ->getName () . '> ' ;
181119 }
182-
183- /**
184- * @return string Obtain tag content considering encoding options.
185- */
186- private function generateContent (): string
187- {
188- $ content = '' ;
189- foreach ($ this ->content as $ item ) {
190- if ($ this ->encode || ($ this ->encode === null && !($ item instanceof NoEncodeStringableInterface))) {
191- $ item = Html::encode ($ item , $ this ->doubleEncode );
192- }
193-
194- $ content .= $ item ;
195- }
196-
197- return $ content ;
198- }
199120}
0 commit comments