Skip to content

Commit 1616fe7

Browse files
authored
Merge pull request #5884 from xlii-chl/develop
script_tag: cosmetic for value-less attributes
2 parents 116f663 + 3e62428 commit 1616fe7

5 files changed

Lines changed: 42 additions & 8 deletions

File tree

system/Helpers/html_helper.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ function doctype(string $type = 'html5'): string
189189
*
190190
* Generates link to a JS file
191191
*
192-
* @param mixed $src Script source or an array
193-
* @param bool $indexPage Should indexPage be added to the JS path
192+
* @param array|string $src Script source or an array of attributes
193+
* @param bool $indexPage Should indexPage be added to the JS path
194194
*/
195195
function script_tag($src = '', bool $indexPage = false): string
196196
{
@@ -207,11 +207,12 @@ function script_tag($src = '', bool $indexPage = false): string
207207
$script .= 'src="' . slash_item('baseURL') . $v . '" ';
208208
}
209209
} else {
210-
$script .= $k . '="' . $v . '" ';
210+
// for attributes without values, like async or defer, use NULL.
211+
$script .= $k . (null === $v ? ' ' : '="' . $v . '" ');
211212
}
212213
}
213214

214-
return $script . 'type="text/javascript"' . '></script>';
215+
return $script . 'type="text/javascript"></script>';
215216
}
216217
}
217218

tests/system/Helpers/HTMLHelperTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,37 @@ public function testScriptTagWithIndexpage()
248248
$this->assertSame($expected, script_tag($target, true));
249249
}
250250

251+
public function testScriptTagWithSrc()
252+
{
253+
$target = ['src' => 'http://site.com/js/mystyles.js'];
254+
$expected = '<script src="http://site.com/js/mystyles.js" type="text/javascript"></script>';
255+
$this->assertSame($expected, script_tag($target));
256+
}
257+
258+
public function testScriptTagWithSrcWithoutProtocol()
259+
{
260+
$target = ['src' => 'js/mystyles.js'];
261+
$expected = '<script src="http://example.com/js/mystyles.js" type="text/javascript"></script>';
262+
$this->assertSame($expected, script_tag($target));
263+
}
264+
265+
public function testScriptTagWithSrcAndAttributes()
266+
{
267+
$target = ['src' => 'js/mystyles.js', 'charset' => 'UTF-8', 'defer' => '', 'async' => null];
268+
$expected = '<script src="http://example.com/js/mystyles.js" charset="UTF-8" defer="" async type="text/javascript"></script>';
269+
$this->assertSame($expected, script_tag($target));
270+
}
271+
272+
/**
273+
* This test has probably no real-world value but may help detecting
274+
* a change in the default behaviour.
275+
*/
276+
public function testScriptTagWithoutAnyArg()
277+
{
278+
$expected = '<script src="http://example.com/" type="text/javascript"></script>';
279+
$this->assertSame($expected, script_tag());
280+
}
281+
251282
public function testLinkTag()
252283
{
253284
$target = 'css/mystyles.css';

user_guide_src/source/changelogs/v4.2.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ Enhancements
4040
- 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> <cleaned filepath>(<line>): <class><function><args>"
4141
- Added support for webp files to **app/Config/Mimes.php**.
4242
- Added 4th parameter ``$includeDir`` to ``get_filenames()``. See :php:func:`get_filenames`.
43+
- HTML helper ``script_tag()`` now uses ``null`` values to write boolean attributes in minimized form: ``<script src="..." defer />``. See the sample code for :php:func:`script_tag`.
4344
- RouteCollection::addRedirect() can now use placeholders.
4445

46+
4547
Changes
4648
*******
4749

user_guide_src/source/helpers/html_helper.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ The following functions are available:
104104

105105
.. php:function:: script_tag([$src = ''[, $indexPage = false]])
106106
107-
:param mixed $src: The source name of a JavaScript file
108-
:param bool $indexPage: Whether to treat ``$src`` as a routed URI string
107+
:param array|string $src: The source name or URL of a JavaScript file, or an associative array specifying the attributes
108+
:param bool $indexPage: Whether to treat ``$src`` as a routed URI string
109109
:returns: HTML script tag
110110
:rtype: string
111111

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
$script = ['src' => 'js/printer.js'];
3+
$script = ['src' => 'js/printer.js', 'defer' => null];
44

55
echo script_tag($script);
6-
// <script src="http://site.com/js/printer.js" type="text/javascript"></script>
6+
// <script src="http://site.com/js/printer.js" defer type="text/javascript"></script>

0 commit comments

Comments
 (0)