1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace tool_brickfield\local\htmlchecker\common\checks;
|
|
|
18 |
|
|
|
19 |
use DOMXPath;
|
|
|
20 |
use tool_brickfield\local\htmlchecker\common\brickfield_accessibility_color_test;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Brickfield accessibility HTML checker library.
|
|
|
24 |
*
|
|
|
25 |
* @package tool_brickfield
|
|
|
26 |
* @copyright 2020 onward: Brickfield Education Labs, www.brickfield.ie
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
*/
|
|
|
29 |
class css_text_has_contrast extends brickfield_accessibility_color_test {
|
|
|
30 |
|
|
|
31 |
/** @var int The default severity code for this test. */
|
|
|
32 |
public $defaultseverity = \tool_brickfield\local\htmlchecker\brickfield_accessibility::BA_TEST_SEVERE;
|
|
|
33 |
|
|
|
34 |
/** @var string The default background color. */
|
|
|
35 |
public $defaultbackground = '#ffffff';
|
|
|
36 |
|
|
|
37 |
/** @var string The default color. */
|
|
|
38 |
public $defaultcolor = '#000000';
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* The main check function. This is called by the parent class to actually check content
|
|
|
42 |
*/
|
|
|
43 |
public function check(): void {
|
|
|
44 |
if (isset($this->options['css_background'])) {
|
|
|
45 |
$this->defaultbackground = $this->options['css_background'];
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
if (isset($this->options['css_foreground'])) {
|
|
|
49 |
$this->defaultcolor = $this->options['css_foreground'];
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
$xpath = new DOMXPath($this->dom);
|
|
|
53 |
|
|
|
54 |
// Selects all nodes that have a style attribute OR 'strong' OR 'em' elements that:
|
|
|
55 |
// Contain only the text in their text nodes
|
|
|
56 |
// OR Have text nodes AND text nodes that are not equal to the string-value of the context node
|
|
|
57 |
// OR Have a text node descendant that equals the string-value of the context node and has no style attributes.
|
|
|
58 |
|
|
|
59 |
$entries = $xpath->query('//*[(text() = . or ( ./*[text() != .]) or (.//*[text() = . and not(@style)]))
|
|
|
60 |
and ((@style) or (name() = "strong") or (name() = "em"))]');
|
|
|
61 |
|
|
|
62 |
foreach ($entries as $element) {
|
|
|
63 |
$style = $this->css->get_style($element);
|
|
|
64 |
|
|
|
65 |
if (isset($style['background-color']) || isset($style['color'])) {
|
|
|
66 |
if (!isset($style['background-color'])) {
|
|
|
67 |
$style['background-color'] = $this->defaultbackground;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
if (!isset($style['color'])) {
|
|
|
71 |
$style['color'] = $this->defaultcolor;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
if ((isset($style['background']) || isset($style['background-color'])) && isset($style['color']) &&
|
|
|
75 |
$element->nodeValue) {
|
|
|
76 |
|
|
|
77 |
$background = (isset($style['background-color'])) ? $style['background-color'] : $style['background'];
|
|
|
78 |
if (!$background || !empty($this->options['css_only_use_default'])) {
|
|
|
79 |
$background = $this->defaultbackground;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
$style['color'] = '#' . $this->convert_color($style['color']);
|
|
|
83 |
$style['background-color'] = '#' . $this->convert_color($background);
|
|
|
84 |
|
|
|
85 |
if (substr($background, 0, 3) == "rgb") {
|
|
|
86 |
$background = '#' . $this->convert_color($background);
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
$luminosity = $this->get_luminosity($style['color'], $background);
|
|
|
90 |
$fontsize = 0;
|
|
|
91 |
$bold = false;
|
|
|
92 |
$italic = false;
|
|
|
93 |
|
|
|
94 |
if (isset($style['font-size'])) {
|
|
|
95 |
$fontsize = $this->get_fontsize($style['font-size']);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
if (isset($style['font-weight'])) {
|
|
|
99 |
preg_match_all('!\d+!', $style['font-weight'], $matches);
|
|
|
100 |
|
|
|
101 |
if (count($matches) > 0) {
|
|
|
102 |
if ($matches >= 700) {
|
|
|
103 |
$bold = true;
|
|
|
104 |
} else {
|
|
|
105 |
if ($style['font-weight'] === 'bold' || $style['font-weight'] === 'bolder') {
|
|
|
106 |
$bold = true;
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
} else if ($element->tagName === "strong") {
|
|
|
111 |
$bold = true;
|
|
|
112 |
$style['font-weight'] = "bold";
|
|
|
113 |
} else {
|
|
|
114 |
$style['font-weight'] = "normal";
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
if (isset($style['font-style'])) {
|
|
|
118 |
if ($style['font-style'] === "italic") {
|
|
|
119 |
$italic = true;
|
|
|
120 |
}
|
|
|
121 |
} else if ($element->tagName === "em") {
|
|
|
122 |
$italic = true;
|
|
|
123 |
$style['font-style'] = "italic";
|
|
|
124 |
} else {
|
|
|
125 |
$style['font-style'] = "normal";
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
if ($element->tagName === 'h1' || $element->tagName === 'h2' || $element->tagName === 'h3' ||
|
|
|
129 |
$element->tagName === 'h4' || $element->tagName === 'h5' || $element->tagName === 'h6' ||
|
|
|
130 |
$fontsize >= 18 || $fontsize >= 14 && $bold) {
|
|
|
131 |
if ($luminosity < 3) {
|
|
|
132 |
$message = 'heading: background-color: ' . $background . '; color:' . $style["color"] .
|
|
|
133 |
'; font-style: ' . $style['font-style'] . '; font-weight: ' . $style['font-weight'] . '; ';
|
|
|
134 |
$this->add_report($element, $message);
|
|
|
135 |
}
|
|
|
136 |
} else {
|
|
|
137 |
if ($luminosity < 4.5) {
|
|
|
138 |
$message = 'text: background-color: ' . $background . '; color:' . $style["color"] . '; font-style: ' .
|
|
|
139 |
$style['font-style'] . '; font-weight: ' . $style['font-weight'] . '; ';
|
|
|
140 |
$this->add_report($element, $message);
|
|
|
141 |
}
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
}
|
|
|
145 |
}
|
|
|
146 |
}
|
|
|
147 |
}
|