mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
FIX Add 'legal empty attributes' to allow empty alt values on i… (#9257)
FIX Add 'legal empty attributes' to allow empty alt values on imgs
This commit is contained in:
commit
3659f2888d
@ -39,6 +39,16 @@ class HTML
|
|||||||
'wbr'
|
'wbr'
|
||||||
];
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List of attributes that should be rendered even if they contain no value
|
||||||
|
*
|
||||||
|
* @config
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private static $legal_empty_attributes = [
|
||||||
|
'alt',
|
||||||
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct and return HTML tag.
|
* Construct and return HTML tag.
|
||||||
*
|
*
|
||||||
@ -52,10 +62,13 @@ class HTML
|
|||||||
$tag = strtolower($tag);
|
$tag = strtolower($tag);
|
||||||
|
|
||||||
// Build list of arguments
|
// Build list of arguments
|
||||||
|
$legalEmptyAttributes = static::config()->get('legal_empty_attributes');
|
||||||
$preparedAttributes = '';
|
$preparedAttributes = '';
|
||||||
foreach ($attributes as $attributeKey => $attributeValue) {
|
foreach ($attributes as $attributeKey => $attributeValue) {
|
||||||
|
$whitelisted = in_array($attributeKey, $legalEmptyAttributes);
|
||||||
|
|
||||||
// Only set non-empty strings (ensures strlen(0) > 0)
|
// Only set non-empty strings (ensures strlen(0) > 0)
|
||||||
if (strlen($attributeValue) > 0) {
|
if (strlen($attributeValue) > 0 || $whitelisted) {
|
||||||
$preparedAttributes .= sprintf(
|
$preparedAttributes .= sprintf(
|
||||||
' %s="%s"',
|
' %s="%s"',
|
||||||
$attributeKey,
|
$attributeKey,
|
||||||
|
@ -45,6 +45,16 @@ class HTMLTest extends SapphireTest
|
|||||||
$this->assertEquals('<a title="HTML & Text">Some <strong>content!</strong></a>', $tag);
|
$this->assertEquals('<a title="HTML & Text">Some <strong>content!</strong></a>', $tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testImgTag()
|
||||||
|
{
|
||||||
|
$tag = HTML::createTag('img', [
|
||||||
|
'src' => 'example.png',
|
||||||
|
'alt' => '',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals('<img src="example.png" alt="" />', $tag);
|
||||||
|
}
|
||||||
|
|
||||||
public function testVoidContentError()
|
public function testVoidContentError()
|
||||||
{
|
{
|
||||||
$this->expectException(InvalidArgumentException::class);
|
$this->expectException(InvalidArgumentException::class);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user