diff --git a/src/Utils/Html.php b/src/Utils/Html.php index 475247d13..f645ac52b 100644 --- a/src/Utils/Html.php +++ b/src/Utils/Html.php @@ -362,17 +362,39 @@ public function getText() } + /** + * @deprecated + */ + public function add($child) + { + trigger_error('Method Nette\Utils\Html::add() is deprecated, use addHtml() or addText() instead.', E_USER_DEPRECATED); + return $this->addHtml($child); + } + + /** * Adds new element's child. * @param Html|string Html node or raw HTML string * @return self */ - public function add($child) + public function addHtml($child) { return $this->insert(NULL, $child); } + /** + * Appends plain-text string to element content. + * @param string plain-text string + * @return self + */ + public function addText($text) + { + $text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8'); + return $this->insert(NULL, $text); + } + + /** * Creates and adds a new Html child. * @param string elements's name diff --git a/tests/Utils/Html.children.phpt b/tests/Utils/Html.children.phpt index 39571f4dc..19f3163b5 100644 --- a/tests/Utils/Html.children.phpt +++ b/tests/Utils/Html.children.phpt @@ -14,7 +14,7 @@ require __DIR__ . '/../bootstrap.php'; test(function () { // add $el = Html::el('ul'); $el->create('li')->setText('one'); - $el->add(Html::el('li')->setText('two'))->class('hello'); + $el->addHtml(Html::el('li')->setText('two'))->class('hello'); Assert::same('
one
two
', (string) $el); + $el->addHtml(Html::el('p')->setText('one')); + $el->addText('two
'); + $el->addHtml('three
'); + Assert::same('one
<p>two</p>three
', (string) $el); // ==> Get child: - Assert::true(isset($el[1])); - Assert::same('two
', (string) $el[1]); - Assert::false(isset($el[2])); + Assert::true(isset($el[0])); + Assert::same('one
', (string) $el[0]); + Assert::same('<p>two</p>', (string) $el[1]); + Assert::same('three
', (string) $el[2]); + Assert::false(isset($el[3])); });