Skip to content

Commit 6a3fa3b

Browse files
committed
Html::addText() accepts Html as setText() does
1 parent 461571d commit 6a3fa3b

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/Utils/Html.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,11 @@ final public function addHtml($child)
349349
* Appends plain-text string to element content.
350350
* @return static
351351
*/
352-
public function addText(string $text)
352+
public function addText($text)
353353
{
354-
$text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
354+
if (!$text instanceof self) {
355+
$text = htmlspecialchars((string) $text, ENT_NOQUOTES, 'UTF-8');
356+
}
355357
return $this->insert(null, $text);
356358
}
357359

tests/Utils/Html.basic.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,18 @@ test(function () { // attributes escaping
115115
test(function () { // setText vs. setHtml
116116
Assert::same('<p>Hello &amp;ndash; World</p>', (string) Html::el('p')->setText('Hello &ndash; World'));
117117
Assert::same('<p>Hello &ndash; World</p>', (string) Html::el('p')->setHtml('Hello &ndash; World'));
118+
119+
Assert::same('<p><br></p>', (string) Html::el('p')->setText(Html::el('br')));
120+
Assert::same('<p><br></p>', (string) Html::el('p')->setHtml(Html::el('br')));
121+
});
122+
123+
124+
test(function () { // addText vs. addHtml
125+
Assert::same('<p>Hello &amp;ndash; World</p>', (string) Html::el('p')->addText('Hello &ndash; World'));
126+
Assert::same('<p>Hello &ndash; World</p>', (string) Html::el('p')->addHtml('Hello &ndash; World'));
127+
128+
Assert::same('<p><br></p>', (string) Html::el('p')->addText(Html::el('br')));
129+
Assert::same('<p><br></p>', (string) Html::el('p')->addHtml(Html::el('br')));
118130
});
119131

120132

0 commit comments

Comments
 (0)