From f864bb63a9f9ecc802e5057c3c9e3baf07d5683b Mon Sep 17 00:00:00 2001 From: Nikola Posa Date: Sat, 27 Sep 2014 21:03:20 +0200 Subject: [PATCH 1/4] Fixing trailing spaces. --- src/Helper/HtmlTag.php | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Helper/HtmlTag.php b/src/Helper/HtmlTag.php index b5e97597..9b0dee93 100644 --- a/src/Helper/HtmlTag.php +++ b/src/Helper/HtmlTag.php @@ -10,23 +10,23 @@ namespace Zend\View\Helper; /** - * Renders tag (both opening and closing) of a web page, to which some custom + * Renders tag (both opening and closing) of a web page, to which some custom * attributes can be added dynamically. - * + * * @author Nikola Posa */ class HtmlTag extends AbstractHtmlElement { /** * Attributes for the tag. - * - * @var array + * + * @var array */ protected $attributes = array(); - + /** * Retrieve object instance; optionally add attributes. - * + * * @param array $attribs * @return \Zend\View\Helper\HtmlTag */ @@ -35,13 +35,13 @@ public function __invoke(array $attribs = array()) if (!empty($attribs)) { $this->addAttributes($attribs); } - + return $this; } - + /** * Set new attribute. - * + * * @param string $attrName * @param string $attrValue * @return \Zend\View\Helper\HtmlTag @@ -51,10 +51,10 @@ public function setAttribute($attrName, $attrValue) $this->attributes[$attrName] = $attrValue; return $this; } - + /** * Add set of attributes. - * + * * @param array $attribs * @return \Zend\View\Helper\HtmlTag */ @@ -65,7 +65,7 @@ public function addAttributes(array $attribs) } return $this; } - + /** * @return array */ @@ -76,17 +76,17 @@ public function getAttributes() /** * Render opening tag. - * + * * @return string */ public function openTag() { return sprintf('', $this->htmlAttribs($this->attributes)); } - + /** * Render closing tag. - * + * * @return string */ public function closeTag() From f52a0ee163b4b672a0147732d4ea1c89f4c836a2 Mon Sep 17 00:00:00 2001 From: Nikola Posa Date: Sun, 28 Sep 2014 11:39:27 +0200 Subject: [PATCH 2/4] Renamed HtmlTag::addAttributes() method to HtmlTag::setAttributes(). Also added new test case for that method. --- src/Helper/HtmlTag.php | 12 +++++------ test/Helper/HtmlTagTest.php | 40 ++++++++++++++++++++++++++----------- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/Helper/HtmlTag.php b/src/Helper/HtmlTag.php index 9b0dee93..6fd02f85 100644 --- a/src/Helper/HtmlTag.php +++ b/src/Helper/HtmlTag.php @@ -28,12 +28,12 @@ class HtmlTag extends AbstractHtmlElement * Retrieve object instance; optionally add attributes. * * @param array $attribs - * @return \Zend\View\Helper\HtmlTag + * @return self */ public function __invoke(array $attribs = array()) { if (!empty($attribs)) { - $this->addAttributes($attribs); + $this->setAttributes($attribs); } return $this; @@ -44,7 +44,7 @@ public function __invoke(array $attribs = array()) * * @param string $attrName * @param string $attrValue - * @return \Zend\View\Helper\HtmlTag + * @return self */ public function setAttribute($attrName, $attrValue) { @@ -53,12 +53,12 @@ public function setAttribute($attrName, $attrValue) } /** - * Add set of attributes. + * Add new or overwrite the existing attributes. * * @param array $attribs - * @return \Zend\View\Helper\HtmlTag + * @return self */ - public function addAttributes(array $attribs) + public function setAttributes(array $attribs) { foreach ($attribs as $name => $value) { $this->setAttribute($name, $value); diff --git a/test/Helper/HtmlTagTest.php b/test/Helper/HtmlTagTest.php index 6fc51953..aa7a4b5a 100644 --- a/test/Helper/HtmlTagTest.php +++ b/test/Helper/HtmlTagTest.php @@ -34,7 +34,7 @@ public function tearDown() { unset($this->helper); } - + protected function assertAttribute($name, $value = null) { $attributes = $this->helper->getAttributes(); @@ -49,44 +49,60 @@ 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->addAttributes($attribs); - + $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('', $this->helper->openTag()); } - + public function testRenderingOpenTagWithAttributes() { $attribs = array( 'xmlns' => 'http://www.w3.org/1999/xhtml', 'xmlns:og' => 'http://ogp.me/ns#', ); - - $this->helper->addAttributes($attribs); - + + $this->helper->setAttributes($attribs); + $tag = $this->helper->openTag(); - + $this->assertStringStartsWith('view->plugin('escapehtmlattr'); foreach ($attribs as $name => $value) { $this->assertContains(sprintf('%s="%s"', $name, $escape($value)), $tag); } } - + public function testRenderingCloseTag() { $this->assertEquals('', $this->helper->closeTag()); From 88e1fe080884d1a6e02bb0ed9effbfb35847f887 Mon Sep 17 00:00:00 2001 From: Nikola Posa Date: Fri, 5 Dec 2014 20:19:27 +0100 Subject: [PATCH 3/4] Added ability for automatically pre-setting approrpiate attributes for the html tag based on currently set doctype. --- src/Helper/HtmlTag.php | 51 +++++++++++++++++++++++++++++++++++++ test/Helper/HtmlTagTest.php | 26 +++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/src/Helper/HtmlTag.php b/src/Helper/HtmlTag.php index 6fd02f85..b56858ff 100644 --- a/src/Helper/HtmlTag.php +++ b/src/Helper/HtmlTag.php @@ -24,6 +24,18 @@ class HtmlTag extends AbstractHtmlElement */ protected $attributes = array(); + /** + * Whether to add appropriate attributes in accordance with currently set DOCTYPE. + * + * @var bool + */ + protected $addDoctypeAttributes = false; + + /** + * @var bool + */ + private $doctypeAttribsAdded = false; + /** * Retrieve object instance; optionally add attributes. * @@ -74,6 +86,24 @@ public function getAttributes() return $this->attributes; } + /** + * @param bool $addDoctypeAttributes + * @return self + */ + public function setAddDoctypeAttributes($addDoctypeAttributes) + { + $this->addDoctypeAttributes = (bool) $addDoctypeAttributes; + return $this; + } + + /** + * @return bool + */ + public function getAddDoctypeAttributes() + { + return $this->addDoctypeAttributes; + } + /** * Render opening tag. * @@ -81,9 +111,30 @@ public function getAttributes() */ public function openTag() { + $this->handleDoctypeAttributes(); + return sprintf('', $this->htmlAttribs($this->attributes)); } + protected function handleDoctypeAttributes() + { + if ($this->addDoctypeAttributes && !$this->doctypeAttribsAdded) { + 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->doctypeAttribsAdded = true; + } + } + /** * Render closing tag. * diff --git a/test/Helper/HtmlTagTest.php b/test/Helper/HtmlTagTest.php index aa7a4b5a..509dbc80 100644 --- a/test/Helper/HtmlTagTest.php +++ b/test/Helper/HtmlTagTest.php @@ -107,4 +107,30 @@ public function testRenderingCloseTag() { $this->assertEquals('', $this->helper->closeTag()); } + + public function testDoctypeAttributesSetter() + { + $this->helper->setAddDoctypeAttributes(true); + $this->assertTrue($this->helper->getAddDoctypeAttributes()); + } + + public function testAppropriateDoctypeAttributesArePresetIfFlagIsOn() + { + $this->view->plugin('doctype')->setDoctype('xhtml'); + + $attribs = array( + 'prefix' => 'og: http://ogp.me/ns#', + ); + + $this->helper->setAddDoctypeAttributes(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); + } + } } From daddb10619d04b28209b2db18c83ce9e2a8bf99a Mon Sep 17 00:00:00 2001 From: Nikola Posa Date: Fri, 5 Dec 2014 21:07:44 +0100 Subject: [PATCH 4/4] Choosing a better naming for the funtonality for pre-setting NS attributes. --- src/Helper/HtmlTag.php | 27 ++++++++++++++------------- test/Helper/HtmlTagTest.php | 10 +++++----- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/Helper/HtmlTag.php b/src/Helper/HtmlTag.php index b56858ff..cc9c764a 100644 --- a/src/Helper/HtmlTag.php +++ b/src/Helper/HtmlTag.php @@ -25,16 +25,17 @@ class HtmlTag extends AbstractHtmlElement protected $attributes = array(); /** - * Whether to add appropriate attributes in accordance with currently set DOCTYPE. + * Whether to pre-set appropriate attributes in accordance + * with the currently set DOCTYPE. * * @var bool */ - protected $addDoctypeAttributes = false; + protected $useNamespaces = false; /** * @var bool */ - private $doctypeAttribsAdded = false; + private $handledNamespaces = false; /** * Retrieve object instance; optionally add attributes. @@ -87,21 +88,21 @@ public function getAttributes() } /** - * @param bool $addDoctypeAttributes + * @param bool $useNamespaces * @return self */ - public function setAddDoctypeAttributes($addDoctypeAttributes) + public function setUseNamespaces($useNamespaces) { - $this->addDoctypeAttributes = (bool) $addDoctypeAttributes; + $this->useNamespaces = (bool) $useNamespaces; return $this; } /** * @return bool */ - public function getAddDoctypeAttributes() + public function getUseNamespaces() { - return $this->addDoctypeAttributes; + return $this->useNamespaces; } /** @@ -111,14 +112,14 @@ public function getAddDoctypeAttributes() */ public function openTag() { - $this->handleDoctypeAttributes(); - + $this->handleNamespaceAttributes(); + return sprintf('', $this->htmlAttribs($this->attributes)); } - protected function handleDoctypeAttributes() + protected function handleNamespaceAttributes() { - if ($this->addDoctypeAttributes && !$this->doctypeAttribsAdded) { + if ($this->useNamespaces && !$this->handledNamespaces) { if (method_exists($this->view, 'plugin')) { $doctypeAttributes = array(); @@ -131,7 +132,7 @@ protected function handleDoctypeAttributes() } } - $this->doctypeAttribsAdded = true; + $this->handledNamespaces = true; } } diff --git a/test/Helper/HtmlTagTest.php b/test/Helper/HtmlTagTest.php index 509dbc80..bdfbf67c 100644 --- a/test/Helper/HtmlTagTest.php +++ b/test/Helper/HtmlTagTest.php @@ -108,13 +108,13 @@ public function testRenderingCloseTag() $this->assertEquals('', $this->helper->closeTag()); } - public function testDoctypeAttributesSetter() + public function testUseNamespacesSetter() { - $this->helper->setAddDoctypeAttributes(true); - $this->assertTrue($this->helper->getAddDoctypeAttributes()); + $this->helper->setUseNamespaces(true); + $this->assertTrue($this->helper->getUseNamespaces()); } - public function testAppropriateDoctypeAttributesArePresetIfFlagIsOn() + public function testAppropriateNamespaceAttributesAreSetIfFlagIsOn() { $this->view->plugin('doctype')->setDoctype('xhtml'); @@ -122,7 +122,7 @@ public function testAppropriateDoctypeAttributesArePresetIfFlagIsOn() 'prefix' => 'og: http://ogp.me/ns#', ); - $this->helper->setAddDoctypeAttributes(true)->setAttributes($attribs); + $this->helper->setUseNamespaces(true)->setAttributes($attribs); $tag = $this->helper->openTag();