Skip to content

Commit

Permalink
Merge pull request #5884 from xlii-chl/develop
Browse files Browse the repository at this point in the history
script_tag: cosmetic for value-less attributes
  • Loading branch information
kenjis authored Apr 25, 2022
2 parents 116f663 + 3e62428 commit 1616fe7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
9 changes: 5 additions & 4 deletions system/Helpers/html_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -207,11 +207,12 @@ function script_tag($src = '', bool $indexPage = false): string
$script .= 'src="' . slash_item('baseURL') . $v . '" ';
}
} else {
$script .= $k . '="' . $v . '" ';
// for attributes without values, like async or defer, use NULL.
$script .= $k . (null === $v ? ' ' : '="' . $v . '" ');
}
}

return $script . 'type="text/javascript"' . '></script>';
return $script . 'type="text/javascript"></script>';
}
}

Expand Down
31 changes: 31 additions & 0 deletions tests/system/Helpers/HTMLHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,37 @@ public function testScriptTagWithIndexpage()
$this->assertSame($expected, script_tag($target, true));
}

public function testScriptTagWithSrc()
{
$target = ['src' => 'http://site.com/js/mystyles.js'];
$expected = '<script src="http://site.com/js/mystyles.js" type="text/javascript"></script>';
$this->assertSame($expected, script_tag($target));
}

public function testScriptTagWithSrcWithoutProtocol()
{
$target = ['src' => 'js/mystyles.js'];
$expected = '<script src="http://example.com/js/mystyles.js" type="text/javascript"></script>';
$this->assertSame($expected, script_tag($target));
}

public function testScriptTagWithSrcAndAttributes()
{
$target = ['src' => 'js/mystyles.js', 'charset' => 'UTF-8', 'defer' => '', 'async' => null];
$expected = '<script src="http://example.com/js/mystyles.js" charset="UTF-8" defer="" async type="text/javascript"></script>';
$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 = '<script src="http://example.com/" type="text/javascript"></script>';
$this->assertSame($expected, script_tag());
}

public function testLinkTag()
{
$target = 'css/mystyles.css';
Expand Down
2 changes: 2 additions & 0 deletions user_guide_src/source/changelogs/v4.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ 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> <cleaned filepath>(<line>): <class><function><args>"
- 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: ``<script src="..." defer />``. See the sample code for :php:func:`script_tag`.
- RouteCollection::addRedirect() can now use placeholders.


Changes
*******

Expand Down
4 changes: 2 additions & 2 deletions user_guide_src/source/helpers/html_helper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ The following functions are available:

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

Expand Down
4 changes: 2 additions & 2 deletions user_guide_src/source/helpers/html_helper/011.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

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

0 comments on commit 1616fe7

Please sign in to comment.