Skip to content

Commit 7f59506

Browse files
committed
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
1 parent 1f9af01 commit 7f59506

3 files changed

Lines changed: 26 additions & 4 deletions

File tree

system/Helpers/html_helper.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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 . (is_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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,27 @@ 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+
251272
public function testLinkTag()
252273
{
253274
$target = 'css/mystyles.css';
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)