Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Html:add() to Html:addHtml() #111

Merged
merged 1 commit into from
Jun 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how is the add() short. What about to allow HTML instance and strings consider as a text? Not sure how to warn about change.

{
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe only allow Html instance?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure that's a good idea. How would you provide custom HTML as string?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You would need to wrap it as an Html instance. But I think that the current solution is better.

{
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