[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Rule/ -> CssIdentifierRule.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2020 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Form\Rule;
  11  
  12  use Joomla\CMS\Form\Form;
  13  use Joomla\CMS\Form\FormRule;
  14  use Joomla\Registry\Registry;
  15  
  16  // phpcs:disable PSR1.Files.SideEffects
  17  \defined('JPATH_PLATFORM') or die;
  18  // phpcs:enable PSR1.Files.SideEffects
  19  
  20  /**
  21   * Form Rule class for the Joomla Platform.
  22   *
  23   * @since  4.0.0
  24   */
  25  class CssIdentifierRule extends FormRule
  26  {
  27      /**
  28       * Method to test if the file path is valid
  29       *
  30       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
  31       * @param   mixed              $value    The form field value to validate.
  32       * @param   string             $group    The field name group control value. This acts as an array container for the field.
  33       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
  34       *                                       full field name would end up being "bar[foo]".
  35       * @param   Registry           $input    An optional Registry object with the entire data set to validate against the entire form.
  36       * @param   Form               $form     The form object for which the field is being tested.
  37       *
  38       * @return  boolean  True if the value is valid, false otherwise.
  39       *
  40       * @since   4.0.0
  41       */
  42      public function test(\SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
  43      {
  44          // If the field is empty and not required, the field is valid.
  45          $required = ((string) $element['required'] === 'true' || (string) $element['required'] === 'required');
  46  
  47          if (!$required && empty($value) && $value !== '0') {
  48              return true;
  49          }
  50  
  51          // Make sure we allow multiple classes to be added
  52          $cssIdentifiers = explode(' ', $value);
  53  
  54          foreach ($cssIdentifiers as $i => $identifier) {
  55              /**
  56               * The following regex rules are based on the Html::cleanCssIdentifier method from Drupal
  57               * https://github.com/drupal/drupal/blob/8.8.5/core/lib/Drupal/Component/Utility/Html.php#L116-L130
  58               *
  59               * with the addition for Joomla that we allow the colon (U+003A).
  60               */
  61  
  62              /**
  63               * Valid characters in a CSS identifier are:
  64               * - the hyphen (U+002D)
  65               * - a-z (U+0030 - U+0039)
  66               * - A-Z (U+0041 - U+005A)
  67               * - the underscore (U+005F)
  68               * - the colon (U+003A)
  69               * - 0-9 (U+0061 - U+007A)
  70               * - ISO 10646 characters U+00A1 and higher
  71               */
  72              if (preg_match('/[^\\x{002D}\\x{0030}-\\x{0039}\\x{0041}-\\x{005A}\\x{005F}\\x{003A}\\x{0061}-\\x{007A}\\x{00A1}-\\x{FFFF}]/u', $identifier)) {
  73                  return false;
  74              }
  75  
  76              /**
  77               * Full identifiers cannot start with a digit, two hyphens, or a hyphen followed by a digit.
  78               */
  79              if (preg_match('/^[0-9]/', $identifier) || preg_match('/^(-[0-9])|^(--)/', $identifier)) {
  80                  return false;
  81              }
  82          }
  83  
  84          return true;
  85      }
  86  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer