Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions src/Rule/ImageHasAlt.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
*/
class ImageHasAlt extends BaseRule
{


public function id()
{
return self::class;
Expand All @@ -20,22 +20,15 @@ public function id()
public function check()
{
foreach ($this->getAllElements('img') as $img) {
if (!$img->hasAttribute('alt')
|| $img->getAttribute('alt') == ''
|| $img->getAttribute('alt') == ' ') {
if(!($img->hasAttribute('role')
&& $img->getAttribute('role') == 'presentation')) {
$this->setIssue($img);
}

else if(!($img->hasAttribute('data-decorative')
&& $img->getAttribute('data-decorative') == 'true')) {
if (!$img->hasAttribute('alt') || trim($img->getAttribute('alt')) == '') {
if(!($img->hasAttribute('role') && $img->getAttribute('role') == 'presentation')
&& !($img->hasAttribute('data-decorative') && $img->getAttribute('data-decorative') == 'true')) {
$this->setIssue($img);
}
}
}

return count($this->issues);
}

}
}
12 changes: 11 additions & 1 deletion tests/ImageHasAltTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@ public function testCheckDataDecorativeFalse()

$this->assertEquals(1, $rule->check(), 'Image Has Alt should have one issue.');
}
}

public function testCheckDataDecorativeTrue()
{
$html = '<div><img src="validDecorativeImage.png" data-decorative="true" role="none"></div>';
$dom = new \DOMDocument('1.0', 'utf-8');
$dom->loadHTML($html);
$rule = new ImageHasAlt($dom);

$this->assertEquals(0, $rule->check(), 'Image Has Alt should have no issues.');
}
}