Skip to content

Commit

Permalink
Omitting Script attribute if none or default (case insensitive)
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 19, 2020
1 parent 313e1c7 commit 5d6ebb0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
16 changes: 12 additions & 4 deletions src/Helper/HeadScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class HeadScript extends Placeholder\Container\AbstractStandalone
const FILE = 'FILE';
const SCRIPT = 'SCRIPT';

/**
* @internal
*/
const DEFAULT_SCRIPT_TYPE = 'text/javascript';

/**
* Are arbitrary attributes allowed?
*
Expand Down Expand Up @@ -130,7 +135,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 +176,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 +272,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 +406,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) || strtolower($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
2 changes: 1 addition & 1 deletion src/Helper/InlineScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __invoke(
$spec = null,
$placement = 'APPEND',
array $attrs = [],
$type = 'text/javascript'
$type = self::DEFAULT_SCRIPT_TYPE
) {
return parent::__invoke($mode, $spec, $placement, $attrs, $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->assertNotRegExp('#type="text/javascript"#i', $test);
}
}

0 comments on commit 5d6ebb0

Please sign in to comment.