Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge pull request zendframework/zendframework#6709 from nikolaposa/f…
Browse files Browse the repository at this point in the history
…eature/html_tag_helper

HtmlTag helper
  • Loading branch information
weierophinney committed Feb 23, 2015
172 parents fec85d9 + b694af0 + c64cbb9 + 17f750b + 7eb9c53 + 274ca2b + b273593 + 7b4d532 + f3d9b3a + f7ab865 + d9d9b78 + fefc773 + 83cbc8a + 1b9fd60 + 8a8b989 + 10da323 + e6aba5a + b71d0a2 + 2aa926c + 8405746 + bf1a8de + f16339d + ed1dbc8 + dbf56da + bdb277e + 364ced3 + 9fccdf3 + 40f5963 + 9d7ddea + e1e4aa8 + 870703b + 958decf + e49a08e + a051e7e + b9f6e35 + 2bbef72 + aea30ce + faec0a2 + 098e166 + 51d6372 + 8757ace + 42477b6 + 32cf329 + 0ebadc5 + 188fa4d + a41bbf0 + a29bbbc + 941280d + d691226 + e547023 + d91c234 + 006ef75 + 69d1518 + 7dc0d1a + a995715 + e780008 + e810876 + 9e4090a + 269bf01 + 3a1f2fd + 0cab32d + 704cc7f + e20c9e6 + a0dfc1a + 76b3e1c + fd429e8 + 187ea79 + 2b43ea6 + e3b1be1 + 19f0ef6 + 33bf9c0 + 3729984 + a5ce396 + 698dbe0 + 0efca0b + 08b2a78 + ddaa846 + 7fe4493 + 0da7331 + 7226b1d + 851904c + a2eb795 + f658a03 + de83270 + 806df8a + 4e7ff23 + 409b768 + 1b97191 + d2649e3 + f0162d1 + 6f01416 + a2b3753 + 1786961 + d157fcb + 4444c37 + 192d20c + 811122b + 3a2cf9b + eb2029b + 7a6edab + 8d8a05d + c1ddf21 + 0090b4d + ef80e35 + 59b30de + 4656098 + 377b920 + ccba82c + 5d2770e + 41714a1 + 8adef43 + a78628f + b35fa7a + 3953c79 + aa28e42 + 38f9a49 + 47ed633 + 0a6bf6e + b61d89d + a1fbb6f + 238512b + f40a328 + 62dc143 + 328df3b + e12fe2d + e34a942 + 04e956d + a19a8c2 + ac4b6c0 + d28282e + bd5af3d + b976cb5 + 4feb67b + 7164be7 + 40174df + e4918ae + 1d9f9a2 + c77ae57 + 223b881 + c2f4e25 + e08b166 + e58e548 + 85e6bbd + 0ac2052 + c7ba6af + f443c57 + 1380626 + 5316b6e + a6136d4 + d233be3 + c100a2a + c6a0e2c + e7eef3c + 812f35d + 604cdcd + 5472285 + 6e2f420 + 76c4d1c + c60cca9 + 77f718e + 6bc8c90 + 6ec1d0f + 53d9070 + a67cdeb + 5d412ee + 132380c + 2809ef0 + a883a8d + 4aa93a8 + ab41e15 + 8615bf4 + daddb10 commit d965d92
Showing 3 changed files with 285 additions and 0 deletions.
148 changes: 148 additions & 0 deletions src/Helper/HtmlTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\View\Helper;

/**
* Renders <html> tag (both opening and closing) of a web page, to which some custom
* attributes can be added dynamically.
*
* @author Nikola Posa <[email protected]>
*/
class HtmlTag extends AbstractHtmlElement
{
/**
* Attributes for the <html> tag.
*
* @var array
*/
protected $attributes = array();

/**
* Whether to pre-set appropriate attributes in accordance
* with the currently set DOCTYPE.
*
* @var bool
*/
protected $useNamespaces = false;

/**
* @var bool
*/
private $handledNamespaces = false;

/**
* Retrieve object instance; optionally add attributes.
*
* @param array $attribs
* @return self
*/
public function __invoke(array $attribs = array())
{
if (!empty($attribs)) {
$this->setAttributes($attribs);
}

return $this;
}

/**
* Set new attribute.
*
* @param string $attrName
* @param string $attrValue
* @return self
*/
public function setAttribute($attrName, $attrValue)
{
$this->attributes[$attrName] = $attrValue;
return $this;
}

/**
* Add new or overwrite the existing attributes.
*
* @param array $attribs
* @return self
*/
public function setAttributes(array $attribs)
{
foreach ($attribs as $name => $value) {
$this->setAttribute($name, $value);
}
return $this;
}

/**
* @return array
*/
public function getAttributes()
{
return $this->attributes;
}

/**
* @param bool $useNamespaces
* @return self
*/
public function setUseNamespaces($useNamespaces)
{
$this->useNamespaces = (bool) $useNamespaces;
return $this;
}

/**
* @return bool
*/
public function getUseNamespaces()
{
return $this->useNamespaces;
}

/**
* Render opening tag.
*
* @return string
*/
public function openTag()
{
$this->handleNamespaceAttributes();

return sprintf('<html%s>', $this->htmlAttribs($this->attributes));
}

protected function handleNamespaceAttributes()
{
if ($this->useNamespaces && !$this->handledNamespaces) {
if (method_exists($this->view, 'plugin')) {
$doctypeAttributes = array();

if ($this->view->plugin('doctype')->isXhtml()) {
$doctypeAttributes = array('xmlns' => 'http://www.w3.org/1999/xhtml');
}

if (!empty($doctypeAttributes)) {
$this->attributes = array_merge($doctypeAttributes, $this->attributes);
}
}

$this->handledNamespaces = true;
}
}

/**
* Render closing tag.
*
* @return string
*/
public function closeTag()
{
return '</html>';
}
}
1 change: 1 addition & 0 deletions src/HelperPluginManager.php
Original file line number Diff line number Diff line change
@@ -52,6 +52,7 @@ class HelperPluginManager extends AbstractPluginManager
'escapecss' => 'Zend\View\Helper\EscapeCss',
'escapeurl' => 'Zend\View\Helper\EscapeUrl',
'gravatar' => 'Zend\View\Helper\Gravatar',
'htmltag' => 'Zend\View\Helper\HtmlTag',
'headlink' => 'Zend\View\Helper\HeadLink',
'headmeta' => 'Zend\View\Helper\HeadMeta',
'headscript' => 'Zend\View\Helper\HeadScript',
136 changes: 136 additions & 0 deletions test/Helper/HtmlTagTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\View\Helper;

use Zend\View\Renderer\PhpRenderer as View;
use Zend\View\Helper\HtmlTag;

/**
* @group Zend_View
* @group Zend_View_Helper
*/
class HtmlTagTest extends \PHPUnit_Framework_TestCase
{
/**
* @var HtmlTag
*/
public $helper;

protected function setUp()
{
$this->view = new View();
$this->helper = new HtmlTag();
$this->helper->setView($this->view);
}

public function tearDown()
{
unset($this->helper);
}

protected function assertAttribute($name, $value = null)
{
$attributes = $this->helper->getAttributes();
$this->assertArrayHasKey($name, $attributes);
if ($value) {
$this->assertEquals($value, $attributes[$name]);
}
}

public function testSettingSingleAttribute()
{
$this->helper->setAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
$this->assertAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
}

public function testAddingMultipleAttributes()
{
$attribs = array(
'xmlns' => 'http://www.w3.org/1999/xhtml',
'prefix' => 'og: http://ogp.me/ns#',
);
$this->helper->setAttributes($attribs);

foreach ($attribs as $name => $value) {
$this->assertAttribute($name, $value);
}
}

public function testSettingMultipleAttributesOverwritesExisting()
{
$this->helper->setAttribute('prefix', 'foobar');

$attribs = array(
'xmlns' => 'http://www.w3.org/1999/xhtml',
'prefix' => 'og: http://ogp.me/ns#',
);
$this->helper->setAttributes($attribs);

$this->assertCount(2, $this->helper->getAttributes());
foreach ($attribs as $name => $value) {
$this->assertAttribute($name, $value);
}
}

public function testRenderingOpenTagWithNoAttributes()
{
$this->assertEquals('<html>', $this->helper->openTag());
}

public function testRenderingOpenTagWithAttributes()
{
$attribs = array(
'xmlns' => 'http://www.w3.org/1999/xhtml',
'xmlns:og' => 'http://ogp.me/ns#',
);

$this->helper->setAttributes($attribs);

$tag = $this->helper->openTag();

$this->assertStringStartsWith('<html', $tag);

$escape = $this->view->plugin('escapehtmlattr');
foreach ($attribs as $name => $value) {
$this->assertContains(sprintf('%s="%s"', $name, $escape($value)), $tag);
}
}

public function testRenderingCloseTag()
{
$this->assertEquals('</html>', $this->helper->closeTag());
}

public function testUseNamespacesSetter()
{
$this->helper->setUseNamespaces(true);
$this->assertTrue($this->helper->getUseNamespaces());
}

public function testAppropriateNamespaceAttributesAreSetIfFlagIsOn()
{
$this->view->plugin('doctype')->setDoctype('xhtml');

$attribs = array(
'prefix' => 'og: http://ogp.me/ns#',
);

$this->helper->setUseNamespaces(true)->setAttributes($attribs);

$tag = $this->helper->openTag();

$escape = $this->view->plugin('escapehtmlattr');

$this->assertContains(sprintf('%s="%s"', 'xmlns', $escape('http://www.w3.org/1999/xhtml')), $tag);
foreach ($attribs as $name => $value) {
$this->assertContains(sprintf('%s="%s"', $name, $escape($value)), $tag);
}
}
}

0 comments on commit d965d92

Please sign in to comment.