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

Commit

Permalink
Merge remote-tracking branch 'basz/hotfix/zf2-349'
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanDotPro committed Jun 12, 2012
8 parents 35511de + 192d20c + d2649e3 + f0162d1 + 6f01416 + a2b3753 + 1786961 + 4a7ad28 commit 3a2cf9b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 47 deletions.
67 changes: 21 additions & 46 deletions src/Helper/HeadScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@ class HeadScript extends Placeholder\Container\Standalone
*/
protected $_arbitraryAttributes = false;

/**
* Should scripts be escaped with cdata or html comment tags?
* @var bool
*/
protected $_escapeScript = true;

/**#@+
* Capture type and/or attributes (used for hinting during capture)
* @var string
Expand Down Expand Up @@ -400,38 +394,6 @@ public function arbitraryAttributesAllowed()
return $this->_arbitraryAttributes;
}

/**
* Set flag indicating if scripts should be escaped with one of these
*
* //<!--
* ...your script
* //]]>
*
* or
*
* //<![CDATA[
* ...your script
* //-->
*
* @param bool $flag Set flag
* @return \Zend\View\Helper\HeadScript
*/
public function setEscapeScript($flag)
{
$this->_escapeScript = (bool) $flag;
return $this;
}

/**
* Are arbitrary attributes allowed?
*
* @return bool
*/
public function escapeScript()
{
return $this->_escapeScript;
}

/**
* Create script HTML
*
Expand All @@ -446,8 +408,9 @@ public function itemToString($item, $indent, $escapeStart, $escapeEnd)
$attrString = '';
if (!empty($item->attributes)) {
foreach ($item->attributes as $key => $value) {
if (!$this->arbitraryAttributesAllowed()
&& !in_array($key, $this->_optionalAttributes))
if ((!$this->arbitraryAttributesAllowed()
&& !in_array($key, $this->_optionalAttributes))
|| in_array($key, array('conditional', 'noescape')))
{
continue;
}
Expand All @@ -458,10 +421,25 @@ public function itemToString($item, $indent, $escapeStart, $escapeEnd)
}
}


$addScriptEscape = !(isset($item->attributes['noescape']) && filter_var($item->attributes['noescape'], FILTER_VALIDATE_BOOLEAN));

$type = ($this->_autoEscape) ? $this->_escape($item->type) : $item->type;
$html = '<script type="' . $type . '"' . $attrString . '>';
if (!empty($item->source)) {
$html .= PHP_EOL . $indent . ' ' . $escapeStart . PHP_EOL . $item->source . $indent . ' ' . $escapeEnd . PHP_EOL . $indent;
$html .= PHP_EOL ;

if ($addScriptEscape) {
$html .= $indent . ' ' . $escapeStart . PHP_EOL;
}

$html .= $indent . ' ' . $item->source;

if ($addScriptEscape) {
$html .= $indent . ' ' . $escapeEnd . PHP_EOL;
}

$html .= $indent;
}
$html .= '</script>';

Expand Down Expand Up @@ -495,11 +473,8 @@ public function toString($indent = null)
$useCdata = $this->useCdata ? true : false;
}

$escapeStart = $escapeEnd = '';
if ($this->escapeScript()) {
$escapeStart = ($useCdata) ? '//<![CDATA[' : '//<!--';
$escapeEnd = ($useCdata) ? '//]]>' : '//-->';
}
$escapeStart = ($useCdata) ? '//<![CDATA[' : '//<!--';
$escapeEnd = ($useCdata) ? '//]]>' : '//-->';

$items = array();
$this->getContainer()->ksort();
Expand Down
38 changes: 37 additions & 1 deletion test/Helper/HeadScriptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,5 +430,41 @@ public function testContainerMaintainsCorrectOrderOfItems()

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

public function testConditionalWithAllowArbitraryAttributesDoesNotIncludeConditionalScript()
{
$this->helper->__invoke()->setAllowArbitraryAttributes(true);
$this->helper->__invoke()->appendFile('/js/foo.js', 'text/javascript', array('conditional' => 'lt IE 7'));
$test = $this->helper->__invoke()->toString();

$this->assertNotContains('conditional', $test);
}

public function testNoEscapeWithAllowArbitraryAttributesDoesNotIncludeNoEscapeScript()
{
$this->helper->__invoke()->setAllowArbitraryAttributes(true);
$this->helper->__invoke()->appendScript('// some script', 'text/javascript', array('noescape' => true));
$test = $this->helper->__invoke()->toString();

$this->assertNotContains('noescape', $test);
}

public function testNoEscapeDefaultsToFalse()
{
$this->helper->__invoke()->appendScript('// some script' . PHP_EOL, 'text/javascript', array());
$test = $this->helper->__invoke()->toString();

$this->assertContains('//<!--', $test);
$this->assertContains('//-->', $test);
}

public function testNoEscapeTrue()
{
$this->helper->__invoke()->appendScript('// some script' . PHP_EOL, 'text/javascript', array('noescape' => true));
$test = $this->helper->__invoke()->toString();

$this->assertNotContains('//<!--', $test);
$this->assertNotContains('//-->', $test);
}

}

0 comments on commit 3a2cf9b

Please sign in to comment.