diff --git a/src/Rule/CssTextStyleEmphasize.php b/src/Rule/CssTextStyleEmphasize.php index 727ef40..e1ec606 100644 --- a/src/Rule/CssTextStyleEmphasize.php +++ b/src/Rule/CssTextStyleEmphasize.php @@ -181,7 +181,7 @@ public function check() $default_background = $options['backgroundColor']; $default_color = $options['textColor']; - foreach ($entries as $element) { + foreach ($entries as $element) { if ($element->nodeType !== XML_ELEMENT_NODE) { continue; } @@ -276,7 +276,9 @@ public function check() $style['font-style'] = "normal"; } - if ($element->tagName === 'h1' || $element->tagName === 'h2' || $element->tagName === 'h3' || $element->tagName === 'h4' || $element->tagName === 'h5' || $element->tagName === 'h6' || $font_size >= 18 || $font_size >= 14 && $bold) { + if ($element->tagName === 'h1' || $element->tagName === 'h2' || $element->tagName === 'h3' || $element->tagName === 'h4' || $element->tagName === 'h5' || $element->tagName === 'h6' || $this->checkTextEqualsHeadingText($element)) { + continue; + } elseif ($font_size >= 18 || $font_size >= 14 && $bold) { if ($luminosity >= 3 && !$bold && !$italic) { $this->message["backgroundColor"] = $background; $this->message["color"] = $style["color"]; @@ -301,6 +303,33 @@ public function check() // Helpers + /** + * Returns true if the tag descends from a heading tag and + * contains the same text, or false otherwise. + * @param object $element A DOMElement object + */ + function checkTextEqualsHeadingText($element) + { + $headingAncestor = false; + // Stop when we reach a heading or fail to find one. + for ($i = 1; $i <= 6 && !$headingAncestor; $i++) { + $heading = "h".$i; + $headingAncestor = $this->getElementAncestor($element, $heading); + } + + // The current element is not descended from a heading. + if (!$headingAncestor) { + return false; + } + + if ($element->textContent === $headingAncestor->textContent) { + return true; + } + + return false; + } + + /** * Returns the first ancestor reached of a tag, or false if it hits * the document root or a given tag. diff --git a/tests/CssTextStyleEmphasizeTest.php b/tests/CssTextStyleEmphasizeTest.php index 06e9de0..bebe42f 100644 --- a/tests/CssTextStyleEmphasizeTest.php +++ b/tests/CssTextStyleEmphasizeTest.php @@ -12,7 +12,7 @@ public function testCheckTrue() 'backgroundColor' => '#ffffff', 'textColor' => '#2D3B45' ]; - + $rule = new CssTextStyleEmphasize($dom, $options); $this->assertEquals(0, $rule->check(), 'Css Text Style Emphasize should have no issues.'); @@ -27,13 +27,13 @@ public function testCheckFalse() 'backgroundColor' => '#ffffff', 'textColor' => '#2D3B45' ]; - + $rule = new CssTextStyleEmphasize($dom, $options); $this->assertEquals(1, $rule->check(), 'Css Text Style Emphasize should have two issues.'); } - public function testCheckBackgroundAttributeColorNamePass() + public function testCheckBackgroundAttributeColorNamePass() { $html = $this->getGoodBackgroundContrastColorNameHtml(); $dom = new \DOMDocument('1.0', 'utf-8'); @@ -42,9 +42,55 @@ public function testCheckBackgroundAttributeColorNamePass() 'backgroundColor' => '#ffffff', 'textColor' => '#2D3B45' ]; - + + $rule = new CssTextStyleEmphasize($dom, $options); + + $this->assertEquals(0, $rule->check(), 'CSS Text Style Emphasize should have no issues.'); + } + + public function testCompletelyColoredHeader() + { + $html = '