Skip to content

Commit

Permalink
Omitting Script attribute if none or default
Browse files Browse the repository at this point in the history
Ommiting the script attribute if its either empty or default (text/javascript) and the doctype is set to HTML5.
Referring to: https://html.spec.whatwg.org/multipage/scripting.html#the-script-element

Signed-off-by: Dennis Dobslaf <[email protected]>
  • Loading branch information
DennisDobslaf committed Aug 18, 2020
1 parent 313e1c7 commit 8825d24
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/Helper/HeadScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class HeadScript extends Placeholder\Container\AbstractStandalone
*/
const FILE = 'FILE';
const SCRIPT = 'SCRIPT';
const DEFAULT_SCRIPT_TYPE = 'text/javascript';

/**
* Are arbitrary attributes allowed?
Expand Down Expand Up @@ -130,7 +131,7 @@ public function __invoke(
$spec = null,
$placement = 'APPEND',
array $attrs = [],
$type = 'text/javascript'
$type = self::DEFAULT_SCRIPT_TYPE
) {
if ((null !== $spec) && is_string($spec)) {
$action = ucfirst(strtolower($mode));
Expand Down Expand Up @@ -171,7 +172,7 @@ public function __call($method, $args)

$action = $matches['action'];
$mode = strtolower($matches['mode']);
$type = 'text/javascript';
$type = self::DEFAULT_SCRIPT_TYPE;
$attrs = [];

if ('offsetSet' == $action) {
Expand Down Expand Up @@ -267,7 +268,7 @@ public function toString($indent = null)
*/
public function captureStart(
$captureType = Placeholder\Container\AbstractContainer::APPEND,
$type = 'text/javascript',
$type = self::DEFAULT_SCRIPT_TYPE,
$attrs = []
) {
if ($this->captureLock) {
Expand Down Expand Up @@ -401,7 +402,10 @@ public function itemToString($item, $indent, $escapeStart, $escapeEnd)
$addScriptEscape = ! (isset($item->attributes['noescape'])
&& filter_var($item->attributes['noescape'], FILTER_VALIDATE_BOOLEAN));

if (empty($item->type) && $this->view && $this->view->plugin('doctype')->isHtml5()) {
if ((empty($item->type) || $item->type === self::DEFAULT_SCRIPT_TYPE)
&& $this->view
&& $this->view->plugin('doctype')->isHtml5()
) {
$html = '<script ' . $attrString . '>';
} else {
$type = ($this->autoEscape) ? $this->escapeAttribute($item->type) : $item->type;
Expand Down
14 changes: 14 additions & 0 deletions test/Helper/HeadScriptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,4 +534,18 @@ public function testSupportsAsyncAttribute()
$test = $this->helper->__invoke()->toString();
$this->assertContains('async="', $test);
}

/**
* @group 23
*/
public function testOmitsTypeAttributeIfNoneGivenAndHtml5Doctype()
{
$view = new \Laminas\View\Renderer\PhpRenderer();
$view->plugin('doctype')->setDoctype(\Laminas\View\Helper\Doctype::HTML5);
$this->helper->setView($view);

$this->helper->__invoke()->appendScript('// some script' . PHP_EOL);
$test = $this->helper->__invoke()->toString();
$this->assertNotContains('type="text/javascript"', $test);
}
}

0 comments on commit 8825d24

Please sign in to comment.