Skip to content

Commit

Permalink
Merge pull request #103 from glensc/zf1-future-107
Browse files Browse the repository at this point in the history
[zend-view] Updated HeadScript to allow for proper html5 syntax creation
  • Loading branch information
falkenhawk authored Feb 25, 2024
2 parents c87deae + 53516cf commit 8a47fd8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
21 changes: 18 additions & 3 deletions packages/zend-view/library/Zend/View/Helper/HeadScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ public function arbitraryAttributesAllowed()
*/
public function itemToString($item, $indent, $escapeStart, $escapeEnd)
{
$isHTML5 = $this->view instanceof Zend_View_Abstract && $this->view->doctype()->isHtml5();
$attrString = '';
if (!empty($item->attributes)) {
foreach ($item->attributes as $key => $value) {
Expand All @@ -424,17 +425,31 @@ public function itemToString($item, $indent, $escapeStart, $escapeEnd)
{
continue;
}
if ('defer' == $key) {
if ('defer' === $key && $value !== false) {
$value = 'defer';
}
$attrString .= sprintf(' %s="%s"', $key, ($this->_autoEscape) ? $this->_escape($value) : $value);

if ($value === false) {
continue;
}

if ($isHTML5 && ($value === true || $value === '' || $value === $key)) {
$attrString .= ' ' . $key;
} else {
$attrString .= sprintf(' %s="%s"', $key, ($this->_autoEscape) ? $this->_escape($value) : $value);
}
}
}

$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 ($isHTML5 && $type === 'text/javascript') {
$html = '<script' . $attrString . '>';
} else {
$html = '<script type="' . $type . '"' . $attrString . '>';
}

if (!empty($item->source)) {
$html .= PHP_EOL ;

Expand Down
38 changes: 38 additions & 0 deletions tests/Zend/View/Helper/HeadScriptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,44 @@ public function testConditionalScriptNoIE()
$this->assertContains('<!--[if !IE]><!--><', $test);
$this->assertContains('<!--<![endif]-->', $test);
}

public function testRenderWithHtml5Doctype()
{
$view = new Zend_View();
$view->doctype('HTML5');
$this->helper->setView($view);

// case: defer attribute is true - renders minimized form, charset attribute is non-empty string - renders full form
$this->helper->setFile(
'example.js', 'text/javascript', array('defer' => true, 'charset' => 'utf-8')
);

$result = $this->helper->toString();

$this->assertNotContains('type="text/javascript"', $result);
$this->assertNotContains('defer="', $result);
$this->assertContains(' defer', $result);
$this->assertContains(' charset="utf-8"', $result);

// case: defer attribute is empty string, renders minimized form
$this->helper->setFile(
'example.js', 'text/javascript', array('defer' => '')
);

$result = $this->helper->toString();

$this->assertNotContains('defer="', $result);
$this->assertContains(' defer', $result);

// case: defer attribute is false, skips the attribute
$this->helper->setFile(
'example.js', 'text/javascript', array('defer' => false)
);

$result = $this->helper->toString();

$this->assertNotContains('defer', $result);
}
}

// Call Zend_View_Helper_HeadScriptTest::main() if this source file is executed directly.
Expand Down

0 comments on commit 8a47fd8

Please sign in to comment.