From c455ade9f24a1f99aa81772516764045296b8ca0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Tvrd=C3=ADk?= Date: Fri, 17 Jun 2016 15:13:24 +0200 Subject: [PATCH] Html: renamed add() to addHtml(); added addText() (#111) --- src/Utils/Html.php | 24 +++++++++++++++++++++++- tests/Utils/Html.children.phpt | 17 ++++++++++------- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/Utils/Html.php b/src/Utils/Html.php index 475247d13..98fa3af56 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__ . '() 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('', (string) $el); @@ -31,15 +31,18 @@ test(function () { // add test(function () { $el = Html::el(NULL); - $el->add(Html::el('p')->setText('one')); - $el->add(Html::el('p')->setText('two')); - 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])); });