Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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                          preg_match_all('!\d+!', $style['font-size'], $matches);
  96                          $fontsize = $matches[0][0];
  97                      }
  98  
  99                      if (isset($style['font-weight'])) {
 100                          preg_match_all('!\d+!', $style['font-weight'], $matches);
 101  
 102                          if (count($matches) > 0) {
 103                              if ($matches >= 700) {
 104                                  $bold = true;
 105                              } else {
 106                                  if ($style['font-weight'] === 'bold' || $style['font-weight'] === 'bolder') {
 107                                      $bold = true;
 108                                  }
 109                              }
 110                          }
 111                      } else if ($element->tagName === "strong") {
 112                          $bold = true;
 113                          $style['font-weight'] = "bold";
 114                      } else {
 115                          $style['font-weight'] = "normal";
 116                      }
 117  
 118                      if (isset($style['font-style'])) {
 119                          if ($style['font-style'] === "italic") {
 120                              $italic = true;
 121                          }
 122                      } else if ($element->tagName === "em") {
 123                          $italic = true;
 124                          $style['font-style'] = "italic";
 125                      } else {
 126                          $style['font-style'] = "normal";
 127                      }
 128  
 129                      if ($element->tagName === 'h1' || $element->tagName === 'h2' || $element->tagName === 'h3' ||
 130                          $element->tagName === 'h4' || $element->tagName === 'h5' || $element->tagName === 'h6' ||
 131                          $fontsize >= 18 || $fontsize >= 14 && $bold) {
 132                          if ($luminosity < 3) {
 133                              $message = 'heading: background-color: ' . $background . '; color:' . $style["color"] .
 134                                  '; font-style: ' . $style['font-style'] . '; font-weight: ' . $style['font-weight'] . '; ';
 135                              $this->add_report($element, $message);
 136                          }
 137                      } else {
 138                          if ($luminosity < 4.5) {
 139                              $message = 'text: background-color: ' . $background . '; color:' . $style["color"] . '; font-style: ' .
 140                                  $style['font-style'] . '; font-weight: ' . $style['font-weight'] . '; ';
 141                              $this->add_report($element, $message);
 142                          }
 143                      }
 144                  }
 145              }
 146          }
 147      }
 148  }