Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / lib / htmlpurifier / HTMLPurifier / AttrDef / HTML / Length.php
blobc4f98436ae77a8b01129836f9ea206db6ed1cdaf
1 <?php
3 require_once 'HTMLPurifier/AttrDef.php';
4 require_once 'HTMLPurifier/AttrDef/HTML/Pixels.php';
6 /**
7 * Validates the HTML type length (not to be confused with CSS's length).
8 *
9 * This accepts integer pixels or percentages as lengths for certain
10 * HTML attributes.
13 class HTMLPurifier_AttrDef_HTML_Length extends HTMLPurifier_AttrDef_HTML_Pixels
16 function validate($string, $config, &$context) {
18 $string = trim($string);
19 if ($string === '') return false;
21 $parent_result = parent::validate($string, $config, $context);
22 if ($parent_result !== false) return $parent_result;
24 $length = strlen($string);
25 $last_char = $string[$length - 1];
27 if ($last_char !== '%') return false;
29 $points = substr($string, 0, $length - 1);
31 if (!is_numeric($points)) return false;
33 $points = (int) $points;
35 if ($points < 0) return '0%';
36 if ($points > 100) return '100%';
38 return ((string) $points) . '%';