Skip to content

Commit

Permalink
Merge pull request #2 from AlanFCMV/emphasize-headers
Browse files Browse the repository at this point in the history
Change StyleEmphasize rule to ignore headers that are fully colored
  • Loading branch information
atarisafari authored Oct 4, 2021
2 parents b3dcdf8 + a570053 commit 4191891
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 8 deletions.
33 changes: 31 additions & 2 deletions src/Rule/CssTextStyleEmphasize.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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"];
Expand All @@ -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.
Expand Down
58 changes: 52 additions & 6 deletions tests/CssTextStyleEmphasizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand All @@ -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');
Expand All @@ -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 = '<h2><span style="color: #008000;">This is a heading with color applied</span></h2>';
$dom = new \DOMDocument('1.0', 'utf-8');
$dom->loadHTML($html);
$options = [
'backgroundColor' => '#ffffff',
'textColor' => '#2D3B45'
];

$rule = new CssTextStyleEmphasize($dom, $options);

$this->assertEquals(0, $rule->check(), 'CSS Text Style Emphasize should have no issues.');
}

public function testPartiallyColoredHeader()
{
$html = '<h2>This is a <span style="color: #008000;">heading</span> with only some color applied</h2>';
$dom = new \DOMDocument('1.0', 'utf-8');
$dom->loadHTML($html);
$options = [
'backgroundColor' => '#ffffff',
'textColor' => '#2D3B45'
];

$rule = new CssTextStyleEmphasize($dom, $options);

$this->assertEquals(1, $rule->check(), 'CSS Text Style Emphasize should have one issue.');
}

public function testNestedDifferentColorInHeader()
{
$html = '<h2><span style="color: #008000;">This is a heading with <span style="color: #3366ff;">two colors</span> applied</span></h2>';
$dom = new \DOMDocument('1.0', 'utf-8');
$dom->loadHTML($html);
$options = [
'backgroundColor' => '#ffffff',
'textColor' => '#2D3B45'
];

$rule = new CssTextStyleEmphasize($dom, $options);

$this->assertEquals(0, $rule->check(), 'CSS Text Has Contrast should have no issues.');
$this->assertEquals(1, $rule->check(), 'CSS Text Style Emphasize should have one issue.');
}
}

}

0 comments on commit 4191891

Please sign in to comment.