Skip to content

Commit 4191891

Browse files
authored
Merge pull request #2 from AlanFCMV/emphasize-headers
Change StyleEmphasize rule to ignore headers that are fully colored
2 parents b3dcdf8 + a570053 commit 4191891

File tree

2 files changed

+83
-8
lines changed

2 files changed

+83
-8
lines changed

src/Rule/CssTextStyleEmphasize.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public function check()
181181
$default_background = $options['backgroundColor'];
182182
$default_color = $options['textColor'];
183183

184-
foreach ($entries as $element) {
184+
foreach ($entries as $element) {
185185
if ($element->nodeType !== XML_ELEMENT_NODE) {
186186
continue;
187187
}
@@ -276,7 +276,9 @@ public function check()
276276
$style['font-style'] = "normal";
277277
}
278278

279-
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) {
279+
if ($element->tagName === 'h1' || $element->tagName === 'h2' || $element->tagName === 'h3' || $element->tagName === 'h4' || $element->tagName === 'h5' || $element->tagName === 'h6' || $this->checkTextEqualsHeadingText($element)) {
280+
continue;
281+
} elseif ($font_size >= 18 || $font_size >= 14 && $bold) {
280282
if ($luminosity >= 3 && !$bold && !$italic) {
281283
$this->message["backgroundColor"] = $background;
282284
$this->message["color"] = $style["color"];
@@ -301,6 +303,33 @@ public function check()
301303

302304
// Helpers
303305

306+
/**
307+
* Returns true if the tag descends from a heading tag and
308+
* contains the same text, or false otherwise.
309+
* @param object $element A DOMElement object
310+
*/
311+
function checkTextEqualsHeadingText($element)
312+
{
313+
$headingAncestor = false;
314+
// Stop when we reach a heading or fail to find one.
315+
for ($i = 1; $i <= 6 && !$headingAncestor; $i++) {
316+
$heading = "h".$i;
317+
$headingAncestor = $this->getElementAncestor($element, $heading);
318+
}
319+
320+
// The current element is not descended from a heading.
321+
if (!$headingAncestor) {
322+
return false;
323+
}
324+
325+
if ($element->textContent === $headingAncestor->textContent) {
326+
return true;
327+
}
328+
329+
return false;
330+
}
331+
332+
304333
/**
305334
* Returns the first ancestor reached of a tag, or false if it hits
306335
* the document root or a given tag.

tests/CssTextStyleEmphasizeTest.php

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function testCheckTrue()
1212
'backgroundColor' => '#ffffff',
1313
'textColor' => '#2D3B45'
1414
];
15-
15+
1616
$rule = new CssTextStyleEmphasize($dom, $options);
1717

1818
$this->assertEquals(0, $rule->check(), 'Css Text Style Emphasize should have no issues.');
@@ -27,13 +27,13 @@ public function testCheckFalse()
2727
'backgroundColor' => '#ffffff',
2828
'textColor' => '#2D3B45'
2929
];
30-
30+
3131
$rule = new CssTextStyleEmphasize($dom, $options);
3232

3333
$this->assertEquals(1, $rule->check(), 'Css Text Style Emphasize should have two issues.');
3434
}
3535

36-
public function testCheckBackgroundAttributeColorNamePass()
36+
public function testCheckBackgroundAttributeColorNamePass()
3737
{
3838
$html = $this->getGoodBackgroundContrastColorNameHtml();
3939
$dom = new \DOMDocument('1.0', 'utf-8');
@@ -42,9 +42,55 @@ public function testCheckBackgroundAttributeColorNamePass()
4242
'backgroundColor' => '#ffffff',
4343
'textColor' => '#2D3B45'
4444
];
45-
45+
46+
$rule = new CssTextStyleEmphasize($dom, $options);
47+
48+
$this->assertEquals(0, $rule->check(), 'CSS Text Style Emphasize should have no issues.');
49+
}
50+
51+
public function testCompletelyColoredHeader()
52+
{
53+
$html = '<h2><span style="color: #008000;">This is a heading with color applied</span></h2>';
54+
$dom = new \DOMDocument('1.0', 'utf-8');
55+
$dom->loadHTML($html);
56+
$options = [
57+
'backgroundColor' => '#ffffff',
58+
'textColor' => '#2D3B45'
59+
];
60+
61+
$rule = new CssTextStyleEmphasize($dom, $options);
62+
63+
$this->assertEquals(0, $rule->check(), 'CSS Text Style Emphasize should have no issues.');
64+
}
65+
66+
public function testPartiallyColoredHeader()
67+
{
68+
$html = '<h2>This is a <span style="color: #008000;">heading</span> with only some color applied</h2>';
69+
$dom = new \DOMDocument('1.0', 'utf-8');
70+
$dom->loadHTML($html);
71+
$options = [
72+
'backgroundColor' => '#ffffff',
73+
'textColor' => '#2D3B45'
74+
];
75+
76+
$rule = new CssTextStyleEmphasize($dom, $options);
77+
78+
$this->assertEquals(1, $rule->check(), 'CSS Text Style Emphasize should have one issue.');
79+
}
80+
81+
public function testNestedDifferentColorInHeader()
82+
{
83+
$html = '<h2><span style="color: #008000;">This is a heading with <span style="color: #3366ff;">two colors</span> applied</span></h2>';
84+
$dom = new \DOMDocument('1.0', 'utf-8');
85+
$dom->loadHTML($html);
86+
$options = [
87+
'backgroundColor' => '#ffffff',
88+
'textColor' => '#2D3B45'
89+
];
90+
4691
$rule = new CssTextStyleEmphasize($dom, $options);
4792

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

0 commit comments

Comments
 (0)