Skip to content

Commit

Permalink
Html: renamed add() to addHtml(); added addText() (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
JanTvrdik authored and dg committed Jun 17, 2016
1 parent 8303fc2 commit c455ade
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
24 changes: 23 additions & 1 deletion src/Utils/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 10 additions & 7 deletions tests/Utils/Html.children.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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('<ul class="hello"><li>one</li><li>two</li></ul>', (string) $el);


Expand All @@ -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('<p>one</p><p>two</p>', (string) $el);
$el->addHtml(Html::el('p')->setText('one'));
$el->addText('<p>two</p>');
$el->addHtml('<p>three</p>');
Assert::same('<p>one</p>&lt;p&gt;two&lt;/p&gt;<p>three</p>', (string) $el);


// ==> Get child:
Assert::true(isset($el[1]));
Assert::same('<p>two</p>', (string) $el[1]);
Assert::false(isset($el[2]));
Assert::true(isset($el[0]));
Assert::same('<p>one</p>', (string) $el[0]);
Assert::same('&lt;p&gt;two&lt;/p&gt;', (string) $el[1]);
Assert::same('<p>three</p>', (string) $el[2]);
Assert::false(isset($el[3]));
});


Expand Down

0 comments on commit c455ade

Please sign in to comment.