mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
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:
parent
1f9af01384
commit
7f595062e2
@ -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>';
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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';
|
||||
|
@ -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>
|
||||
|
Loading…
x
Reference in New Issue
Block a user