Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 
1441 ariadna 65
            if (isset($style['background-color']) || isset($style['color']) || isset($style['background'])) {
1 efrain 66
                if (!isset($style['background-color'])) {
1441 ariadna 67
                    if (isset($style['background'])) {
68
                        // Parsing background-color from CSS background shortcut string.
69
                        $style['background-color'] = '#' . $this->convert_color($style['background']);
70
                        // If value is empty after hash, then use defaultbackground.
71
                        if ($style['background-color'] == '#') {
72
                            $style['background-color'] = $this->defaultbackground;
73
                        }
74
                    } else {
75
                        $style['background-color'] = $this->defaultbackground;
76
                    }
1 efrain 77
                }
78
 
79
                if (!isset($style['color'])) {
80
                    $style['color'] = $this->defaultcolor;
81
                }
82
 
83
                if ((isset($style['background']) || isset($style['background-color'])) && isset($style['color']) &&
84
                    $element->nodeValue) {
85
 
86
                    $background = (isset($style['background-color'])) ? $style['background-color'] : $style['background'];
87
                    if (!$background || !empty($this->options['css_only_use_default'])) {
88
                        $background = $this->defaultbackground;
89
                    }
90
 
91
                    $style['color'] = '#' . $this->convert_color($style['color']);
92
                    $style['background-color'] = '#' . $this->convert_color($background);
93
 
1441 ariadna 94
                    $luminosity = $this->get_luminosity($style['color'], $style['background-color']);
1 efrain 95
                    $fontsize = 0;
96
                    $bold = false;
97
                    $italic = false;
98
 
99
                    if (isset($style['font-size'])) {
100
                        $fontsize = $this->get_fontsize($style['font-size']);
101
                    }
102
 
103
                    if (isset($style['font-weight'])) {
104
                        preg_match_all('!\d+!', $style['font-weight'], $matches);
105
 
106
                        if (count($matches) > 0) {
107
                            if ($matches >= 700) {
108
                                $bold = true;
109
                            } else {
110
                                if ($style['font-weight'] === 'bold' || $style['font-weight'] === 'bolder') {
111
                                    $bold = true;
112
                                }
113
                            }
114
                        }
115
                    } else if ($element->tagName === "strong") {
116
                        $bold = true;
117
                        $style['font-weight'] = "bold";
118
                    } else {
119
                        $style['font-weight'] = "normal";
120
                    }
121
 
122
                    if (isset($style['font-style'])) {
123
                        if ($style['font-style'] === "italic") {
124
                            $italic = true;
125
                        }
126
                    } else if ($element->tagName === "em") {
127
                        $italic = true;
128
                        $style['font-style'] = "italic";
129
                    } else {
130
                        $style['font-style'] = "normal";
131
                    }
132
 
133
                    if ($element->tagName === 'h1' || $element->tagName === 'h2' || $element->tagName === 'h3' ||
134
                        $element->tagName === 'h4' || $element->tagName === 'h5' || $element->tagName === 'h6' ||
135
                        $fontsize >= 18 || $fontsize >= 14 && $bold) {
136
                        if ($luminosity < 3) {
137
                            $message = 'heading: background-color: ' . $background . '; color:' . $style["color"] .
138
                                '; font-style: ' . $style['font-style'] . '; font-weight: ' . $style['font-weight'] . '; ';
139
                            $this->add_report($element, $message);
140
                        }
141
                    } else {
142
                        if ($luminosity < 4.5) {
143
                            $message = 'text: background-color: ' . $background . '; color:' . $style["color"] . '; font-style: ' .
144
                                $style['font-style'] . '; font-weight: ' . $style['font-weight'] . '; ';
145
                            $this->add_report($element, $message);
146
                        }
147
                    }
148
                }
149
            }
150
        }
151
    }
152
}