From b0daf8813fdb3e00f9138089002a7248d76464f5 Mon Sep 17 00:00:00 2001 From: Dennis Dobslaf Date: Tue, 11 Aug 2020 12:44:22 +0200 Subject: [PATCH] Omitting Script attribute if none or default (case insensitive) 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 --- src/Helper/HeadScript.php | 16 ++++++++++++---- src/Helper/InlineScript.php | 2 +- test/Helper/HeadScriptTest.php | 15 +++++++++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/Helper/HeadScript.php b/src/Helper/HeadScript.php index b1b8fb7a5..c9dd73a5f 100644 --- a/src/Helper/HeadScript.php +++ b/src/Helper/HeadScript.php @@ -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? * @@ -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)); @@ -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) { @@ -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) { @@ -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 = '