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

Commit

Permalink
Merge branch 'feature/view-helper-nav-escapeLabel' of https://github.…
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Jul 31, 2012
7 parents 69d1518 + b35fa7a + 27aff48 + b0c89ba + a78628f + 49b6876 + 1997d1d commit 8adef43
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 14 deletions.
63 changes: 49 additions & 14 deletions src/Helper/Navigation/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ class Menu extends AbstractHelper
*/
protected $onlyActiveBranch = false;

/**
* Whether labels should be escaped
*
* @var bool
*/
protected $escapeLabels = true;

/**
* Whether parents should be rendered when only rendering active branch
*
Expand Down Expand Up @@ -119,6 +126,18 @@ public function getOnlyActiveBranch()
return $this->onlyActiveBranch;
}

/**
* Sets a flag indicating whether labels should be escaped
*
* @param bool $flag [optional] escape labels. Default is true.
* @return Menu fluent interface, returns self
*/
public function escapeLabels($flag = true)
{
$this->escapeLabels = (bool) $flag;
return $this;
}

/**
* Enables/disables rendering of parents when only rendering active branch
*
Expand Down Expand Up @@ -184,10 +203,11 @@ public function getPartial()
*
* Overrides {@link AbstractHelper::htmlify()}.
*
* @param AbstractPage $page page to generate HTML for
* @return string HTML string for the given page
* @param AbstractPage $page page to generate HTML for
* @param bool $escapeLabel Whether or not to escape the label
* @return string HTML string for the given page
*/
public function htmlify(AbstractPage $page)
public function htmlify(AbstractPage $page, $escapeLabel = true)
{
// get label and title for translating
$label = $page->getLabel();
Expand Down Expand Up @@ -221,10 +241,16 @@ public function htmlify(AbstractPage $page)
$element = 'span';
}

$escaper = $this->view->plugin('escapeHtml');
return '<' . $element . $this->htmlAttribs($attribs) . '>'
. $escaper($label)
. '</' . $element . '>';
$html = '<' . $element . $this->htmlAttribs($attribs) . '>';
if ($escapeLabel === true) {
$escaper = $this->view->plugin('escapeHtml');
$html .= $escaper($label);
} else {
$html .= $label;
}
$html .= '</' . $element . '>';

return $html;
}

/**
Expand Down Expand Up @@ -271,6 +297,10 @@ protected function normalizeOptions(array $options = array())
$options['onlyActiveBranch'] = $this->getOnlyActiveBranch();
}

if (!isset($options['escapeLabels'])) {
$options['escapeLabels'] = $this->escapeLabels;
}

if (!isset($options['renderParents'])) {
$options['renderParents'] = $this->getRenderParents();
}
Expand All @@ -296,7 +326,8 @@ protected function renderDeepestMenu(AbstractContainer $container,
$ulClass,
$indent,
$minDepth,
$maxDepth
$maxDepth,
$escapeLabels
) {
if (!$active = $this->findActive($container, $minDepth - 1, $maxDepth)) {
return '';
Expand Down Expand Up @@ -324,7 +355,7 @@ protected function renderDeepestMenu(AbstractContainer $container,
}
$liClass = $subPage->isActive(true) ? ' class="active"' : '';
$html .= $indent . ' <li' . $liClass . '>' . self::EOL;
$html .= $indent . ' ' . $this->htmlify($subPage) . self::EOL;
$html .= $indent . ' ' . $this->htmlify($subPage, $escapeLabels) . self::EOL;
$html .= $indent . ' </li>' . self::EOL;
}

Expand All @@ -349,7 +380,8 @@ protected function renderNormalMenu(AbstractContainer $container,
$indent,
$minDepth,
$maxDepth,
$onlyActive
$onlyActive,
$escapeLabels
) {
$html = '';

Expand Down Expand Up @@ -429,7 +461,7 @@ protected function renderNormalMenu(AbstractContainer $container,
// render li tag and page
$liClass = $isActive ? ' class="active"' : '';
$html .= $myIndent . ' <li' . $liClass . '>' . self::EOL
. $myIndent . ' ' . $this->htmlify($page) . self::EOL;
. $myIndent . ' ' . $this->htmlify($page, $escapeLabels) . self::EOL;

// store as previous depth for next iteration
$prevDepth = $depth;
Expand Down Expand Up @@ -478,14 +510,16 @@ public function renderMenu($container = null, array $options = array())
$options['ulClass'],
$options['indent'],
$options['minDepth'],
$options['maxDepth']);
$options['maxDepth'],
$options['escapeLabels']);
} else {
$html = $this->renderNormalMenu($container,
$options['ulClass'],
$options['indent'],
$options['minDepth'],
$options['maxDepth'],
$options['onlyActiveBranch']);
$options['onlyActiveBranch'],
$options['escapeLabels']);
}

return $html;
Expand Down Expand Up @@ -531,7 +565,8 @@ public function renderSubMenu(AbstractContainer $container = null,
'minDepth' => null,
'maxDepth' => null,
'onlyActiveBranch' => true,
'renderParents' => false
'renderParents' => false,
'escapeLabels' => true
));
}

Expand Down
36 changes: 36 additions & 0 deletions test/Helper/Navigation/MenuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,42 @@ public function testSetUlCssClass()
$this->assertEquals($expected, $this->_helper->render($this->_nav2));
}

public function testOptionEscapeLabelsAsTrue()
{
$options = array(
'escapeLabels' => true
);

$container = new \Zend\Navigation\Navigation($this->_nav2->toArray());
$container->addPage(array(
'label' => 'Badges <span class="badge">1</span>',
'uri' => 'badges'
));

$expected = $this->_getExpected('menu/escapelabels_as_true.html');
$actual = $this->_helper->renderMenu($container, $options);

$this->assertEquals($expected, $actual);
}

public function testOptionEscapeLabelsAsFalse()
{
$options = array(
'escapeLabels' => false
);

$container = new \Zend\Navigation\Navigation($this->_nav2->toArray());
$container->addPage(array(
'label' => 'Badges <span class="badge">1</span>',
'uri' => 'badges'
));

$expected = $this->_getExpected('menu/escapelabels_as_false.html');
$actual = $this->_helper->renderMenu($container, $options);

$this->assertEquals($expected, $actual);
}

public function testTranslationUsingZendTranslate()
{
$translator = $this->_getTranslator();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<ul class="navigation">
<li>
<a href="site1">Site 1</a>
</li>
<li class="active">
<a href="site2">Site 2</a>
</li>
<li>
<a href="site3">Site 3</a>
</li>
<li>
<a href="badges">Badges <span class="badge">1</span></a>
</li>
</ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<ul class="navigation">
<li>
<a href="site1">Site 1</a>
</li>
<li class="active">
<a href="site2">Site 2</a>
</li>
<li>
<a href="site3">Site 3</a>
</li>
<li>
<a href="badges">Badges &lt;span class=&quot;badge&quot;&gt;1&lt;/span&gt;</a>
</li>
</ul>

0 comments on commit 8adef43

Please sign in to comment.