script_tag: cosmetic for value-less attributes

Some attributes are usually written without values, for example :
`<script defer async src="..." />`

This is purely cosmetic as the attribute should also be accepted with an empty
value or a value identical to the attribute's name :
https://dev.w3.org/html5/pf-summary/infrastructure.html#boolean-attribute
This commit is contained in:
Chl 2022-04-08 23:31:04 +02:00
parent 1f9af01384
commit 7f595062e2
No known key found for this signature in database
GPG Key ID: 80012B734F21B934
3 changed files with 26 additions and 4 deletions

View File

@ -207,11 +207,12 @@ if (! function_exists('script_tag')) {
$script .= 'src="' . slash_item('baseURL') . $v . '" ';
}
} else {
$script .= $k . '="' . $v . '" ';
// for attributes without values, like async or defer, use NULL.
$script .= $k . (is_null($v) ? ' ' : '="' . $v . '" ');
}
}
return $script . 'type="text/javascript"' . '></script>';
return $script . 'type="text/javascript"></script>';
}
}

View File

@ -248,6 +248,27 @@ final class HTMLHelperTest extends CIUnitTestCase
$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));
}
public function testLinkTag()
{
$target = 'css/mystyles.css';

View File

@ -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>