Skip to content

Commit

Permalink
script_tag: cosmetic for value-less attributes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
xlii-chl committed Apr 8, 2022
1 parent 1f9af01 commit 50a18fe
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
5 changes: 3 additions & 2 deletions system/Helpers/html_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 . (is_null($v) ? ' ' : '="' . $v . '" ');
}
}

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

Expand Down
21 changes: 21 additions & 0 deletions tests/system/Helpers/HTMLHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,27 @@ 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));
}

public function testLinkTag()
{
$target = 'css/mystyles.css';
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 50a18fe

Please sign in to comment.