From 4771ef89f2efe7c70991fbe6b54604fb4fee8457 Mon Sep 17 00:00:00 2001 From: AlanFCMV Date: Thu, 28 Oct 2021 10:23:36 -0400 Subject: [PATCH] Fix imageHasAlt for dataDecorative true --- src/Rule/ImageHasAlt.php | 21 +++++++-------------- tests/ImageHasAltTest.php | 12 +++++++++++- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/Rule/ImageHasAlt.php b/src/Rule/ImageHasAlt.php index bb566c3..61fecf4 100644 --- a/src/Rule/ImageHasAlt.php +++ b/src/Rule/ImageHasAlt.php @@ -10,8 +10,8 @@ */ class ImageHasAlt extends BaseRule { - - + + public function id() { return self::class; @@ -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); } -} \ No newline at end of file +} diff --git a/tests/ImageHasAltTest.php b/tests/ImageHasAltTest.php index b9f01a1..5509598 100644 --- a/tests/ImageHasAltTest.php +++ b/tests/ImageHasAltTest.php @@ -42,4 +42,14 @@ public function testCheckDataDecorativeFalse() $this->assertEquals(1, $rule->check(), 'Image Has Alt should have one issue.'); } -} \ No newline at end of file + + public function testCheckDataDecorativeTrue() + { + $html = '
'; + $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.'); + } +}