From 7f595062e2111b39a10a5300eca45cf8076908e8 Mon Sep 17 00:00:00 2001 From: Chl Date: Fri, 8 Apr 2022 23:31:04 +0200 Subject: [PATCH 1/3] script_tag: cosmetic for value-less attributes Some attributes are usually written without values, for example : `'; + return $script . 'type="text/javascript">'; } } diff --git a/tests/system/Helpers/HTMLHelperTest.php b/tests/system/Helpers/HTMLHelperTest.php index b3ed47d0c3e6..74b087228fdd 100755 --- a/tests/system/Helpers/HTMLHelperTest.php +++ b/tests/system/Helpers/HTMLHelperTest.php @@ -248,6 +248,27 @@ public function testScriptTagWithIndexpage() $this->assertSame($expected, script_tag($target, true)); } + public function testScriptTagWithSrc() + { + $target = ['src' => 'http://site.com/js/mystyles.js']; + $expected = ''; + $this->assertSame($expected, script_tag($target)); + } + + public function testScriptTagWithSrcWithoutProtocol() + { + $target = ['src' => 'js/mystyles.js']; + $expected = ''; + $this->assertSame($expected, script_tag($target)); + } + + public function testScriptTagWithSrcAndAttributes() + { + $target = ['src' => 'js/mystyles.js', 'charset' => 'UTF-8', 'defer' => '', 'async' => null]; + $expected = ''; + $this->assertSame($expected, script_tag($target)); + } + public function testLinkTag() { $target = 'css/mystyles.css'; diff --git a/user_guide_src/source/helpers/html_helper/011.php b/user_guide_src/source/helpers/html_helper/011.php index 126fd86c4952..f47bb7053770 100644 --- a/user_guide_src/source/helpers/html_helper/011.php +++ b/user_guide_src/source/helpers/html_helper/011.php @@ -1,6 +1,6 @@ 'js/printer.js']; +$script = ['src' => 'js/printer.js', 'defer' => null]; echo script_tag($script); -// +// From a8d5b894c9ae17f1a8c8721eae44c0da22bec87c Mon Sep 17 00:00:00 2001 From: Chl Date: Sun, 10 Apr 2022 03:10:15 +0200 Subject: [PATCH 2/3] coding style: cs-fixer is_null() rule --- system/Helpers/html_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Helpers/html_helper.php b/system/Helpers/html_helper.php index 0fc2f110399f..a15b18ff47e8 100755 --- a/system/Helpers/html_helper.php +++ b/system/Helpers/html_helper.php @@ -208,7 +208,7 @@ function script_tag($src = '', bool $indexPage = false): string } } else { // for attributes without values, like async or defer, use NULL. - $script .= $k . (is_null($v) ? ' ' : '="' . $v . '" '); + $script .= $k . (null === $v ? ' ' : '="' . $v . '" '); } } From 4fd7ac58307590112e6bdb6add99c1d9d085e57f Mon Sep 17 00:00:00 2001 From: Chl Date: Mon, 18 Apr 2022 16:43:20 +0200 Subject: [PATCH 3/3] script_tag: changelog, doc + test with default arg --- system/Helpers/html_helper.php | 4 ++-- tests/system/Helpers/HTMLHelperTest.php | 10 ++++++++++ user_guide_src/source/changelogs/v4.2.0.rst | 1 + user_guide_src/source/helpers/html_helper.rst | 4 ++-- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/system/Helpers/html_helper.php b/system/Helpers/html_helper.php index a15b18ff47e8..c2e48d890fef 100755 --- a/system/Helpers/html_helper.php +++ b/system/Helpers/html_helper.php @@ -189,8 +189,8 @@ function doctype(string $type = 'html5'): string * * Generates link to a JS file * - * @param mixed $src Script source or an array - * @param bool $indexPage Should indexPage be added to the JS path + * @param array|string $src Script source or an array of attributes + * @param bool $indexPage Should indexPage be added to the JS path */ function script_tag($src = '', bool $indexPage = false): string { diff --git a/tests/system/Helpers/HTMLHelperTest.php b/tests/system/Helpers/HTMLHelperTest.php index 74b087228fdd..d539d7421be6 100755 --- a/tests/system/Helpers/HTMLHelperTest.php +++ b/tests/system/Helpers/HTMLHelperTest.php @@ -269,6 +269,16 @@ public function testScriptTagWithSrcAndAttributes() $this->assertSame($expected, script_tag($target)); } + /** + * This test has probably no real-world value but may help detecting + * a change in the default behaviour. + */ + public function testScriptTagWithoutAnyArg() + { + $expected = ''; + $this->assertSame($expected, script_tag()); + } + public function testLinkTag() { $target = 'css/mystyles.css'; diff --git a/user_guide_src/source/changelogs/v4.2.0.rst b/user_guide_src/source/changelogs/v4.2.0.rst index 0ed69ab0760a..fa88608dc355 100644 --- a/user_guide_src/source/changelogs/v4.2.0.rst +++ b/user_guide_src/source/changelogs/v4.2.0.rst @@ -38,6 +38,7 @@ Enhancements - The log format has also changed. If users are depending on the log format in their apps, the new log format is "<1-based count> (): " - Added support for webp files to **app/Config/Mimes.php**. - Added 4th parameter ``$includeDir`` to ``get_filenames()``. See :php:func:`get_filenames`. +- HTML helper ``script_tag()`` now uses ``null`` values to write boolean attributes in minimized form: ``