Skip to content

Commit ab1d0b8

Browse files
committed
Html: accepts IHtmlString [Closes nette/forms#160]
1 parent 6a3fa3b commit ab1d0b8

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/Utils/Html.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ public function data(string $name, $value = null)
280280

281281
/**
282282
* Sets element's HTML content.
283+
* @param IHtmlString|string
283284
* @return static
284285
* @throws Nette\InvalidArgumentException
285286
*/
@@ -313,12 +314,13 @@ final public function getHtml(): string
313314

314315
/**
315316
* Sets element's textual content.
317+
* @param IHtmlString|string
316318
* @return static
317319
* @throws Nette\InvalidArgumentException
318320
*/
319321
final public function setText($text)
320322
{
321-
if (!is_array($text) && !$text instanceof self) {
323+
if (!$text instanceof IHtmlString) {
322324
$text = htmlspecialchars((string) $text, ENT_NOQUOTES, 'UTF-8');
323325
}
324326
return $this->setHtml((string) $text);
@@ -336,7 +338,7 @@ final public function getText(): string
336338

337339
/**
338340
* Adds new element's child.
339-
* @param Html|string Html node or raw HTML string
341+
* @param IHtmlString|string Html node or raw HTML string
340342
* @return static
341343
*/
342344
final public function addHtml($child)
@@ -347,11 +349,12 @@ final public function addHtml($child)
347349

348350
/**
349351
* Appends plain-text string to element content.
352+
* @param IHtmlString|string
350353
* @return static
351354
*/
352355
public function addText($text)
353356
{
354-
if (!$text instanceof self) {
357+
if (!$text instanceof IHtmlString) {
355358
$text = htmlspecialchars((string) $text, ENT_NOQUOTES, 'UTF-8');
356359
}
357360
return $this->insert(null, $text);
@@ -372,13 +375,14 @@ final public function create(string $name, $attrs = null)
372375

373376
/**
374377
* Inserts child node.
375-
* @param Html|string $child Html node or raw HTML string
378+
* @param IHtmlString|string $child Html node or raw HTML string
376379
* @return static
377380
* @throws Nette\InvalidArgumentException
378381
*/
379382
public function insert(int $index = null, $child, bool $replace = false)
380383
{
381-
if ($child instanceof self || is_scalar($child)) {
384+
if ($child instanceof IHtmlString || is_scalar($child)) {
385+
$child = $child instanceof self ? $child : (string) $child;
382386
if ($index === null) { // append
383387
$this->children[] = $child;
384388

tests/Utils/Html.basic.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,23 @@ test(function () { // attributes escaping
112112
});
113113

114114

115+
class BR implements Nette\Utils\IHtmlString
116+
{
117+
public function __toString(): string
118+
{
119+
return '<br>';
120+
}
121+
}
122+
115123
test(function () { // setText vs. setHtml
116124
Assert::same('<p>Hello &amp;ndash; World</p>', (string) Html::el('p')->setText('Hello &ndash; World'));
117125
Assert::same('<p>Hello &ndash; World</p>', (string) Html::el('p')->setHtml('Hello &ndash; World'));
118126

119127
Assert::same('<p><br></p>', (string) Html::el('p')->setText(Html::el('br')));
120128
Assert::same('<p><br></p>', (string) Html::el('p')->setHtml(Html::el('br')));
129+
130+
Assert::same('<p><br></p>', (string) Html::el('p')->setText(new BR));
131+
Assert::same('<p><br></p>', (string) Html::el('p')->setHtml(new BR));
121132
});
122133

123134

@@ -127,6 +138,9 @@ test(function () { // addText vs. addHtml
127138

128139
Assert::same('<p><br></p>', (string) Html::el('p')->addText(Html::el('br')));
129140
Assert::same('<p><br></p>', (string) Html::el('p')->addHtml(Html::el('br')));
141+
142+
Assert::same('<p><br></p>', (string) Html::el('p')->addText(new BR));
143+
Assert::same('<p><br></p>', (string) Html::el('p')->addHtml(new BR));
130144
});
131145

132146

0 commit comments

Comments
 (0)