Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

script_tag(): cosmetic for value-less attributes #5884

Merged
merged 4 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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];
paulbalandan marked this conversation as resolved.
Show resolved Hide resolved
$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
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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> <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`.

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>