Skip to content

Commit 4771ef8

Browse files
committed
Fix imageHasAlt for dataDecorative true
1 parent fa4ad3c commit 4771ef8

2 files changed

Lines changed: 18 additions & 15 deletions

File tree

src/Rule/ImageHasAlt.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
*/
1111
class ImageHasAlt extends BaseRule
1212
{
13-
14-
13+
14+
1515
public function id()
1616
{
1717
return self::class;
@@ -20,22 +20,15 @@ public function id()
2020
public function check()
2121
{
2222
foreach ($this->getAllElements('img') as $img) {
23-
if (!$img->hasAttribute('alt')
24-
|| $img->getAttribute('alt') == ''
25-
|| $img->getAttribute('alt') == ' ') {
26-
if(!($img->hasAttribute('role')
27-
&& $img->getAttribute('role') == 'presentation')) {
28-
$this->setIssue($img);
29-
}
30-
31-
else if(!($img->hasAttribute('data-decorative')
32-
&& $img->getAttribute('data-decorative') == 'true')) {
23+
if (!$img->hasAttribute('alt') || trim($img->getAttribute('alt')) == '') {
24+
if(!($img->hasAttribute('role') && $img->getAttribute('role') == 'presentation')
25+
&& !($img->hasAttribute('data-decorative') && $img->getAttribute('data-decorative') == 'true')) {
3326
$this->setIssue($img);
3427
}
3528
}
3629
}
37-
30+
3831
return count($this->issues);
3932
}
4033

41-
}
34+
}

tests/ImageHasAltTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,14 @@ public function testCheckDataDecorativeFalse()
4242

4343
$this->assertEquals(1, $rule->check(), 'Image Has Alt should have one issue.');
4444
}
45-
}
45+
46+
public function testCheckDataDecorativeTrue()
47+
{
48+
$html = '<div><img src="validDecorativeImage.png" data-decorative="true" role="none"></div>';
49+
$dom = new \DOMDocument('1.0', 'utf-8');
50+
$dom->loadHTML($html);
51+
$rule = new ImageHasAlt($dom);
52+
53+
$this->assertEquals(0, $rule->check(), 'Image Has Alt should have no issues.');
54+
}
55+
}

0 commit comments

Comments
 (0)