Whoops, forgot to edit WHATSNEW
[htmlpurifier.git] / library / HTMLPurifier / AttrDef / HTML / Pixels.php
bloba1d019e095b62ad3186a847f60f716a9eb10f868
1 <?php
3 /**
4 * Validates an integer representation of pixels according to the HTML spec.
5 */
6 class HTMLPurifier_AttrDef_HTML_Pixels extends HTMLPurifier_AttrDef
9 /**
10 * @type int
12 protected $max;
14 /**
15 * @param int $max
17 public function __construct($max = null)
19 $this->max = $max;
22 /**
23 * @param string $string
24 * @param HTMLPurifier_Config $config
25 * @param HTMLPurifier_Context $context
26 * @return bool|string
28 public function validate($string, $config, $context)
30 $string = trim($string);
31 if ($string === '0') {
32 return $string;
34 if ($string === '') {
35 return false;
37 $length = strlen($string);
38 if (substr($string, $length - 2) == 'px') {
39 $string = substr($string, 0, $length - 2);
41 if (!is_numeric($string)) {
42 return false;
44 $int = (int)$string;
46 if ($int < 0) {
47 return '0';
50 // upper-bound value, extremely high values can
51 // crash operating systems, see <http://ha.ckers.org/imagecrash.html>
52 // WARNING, above link WILL crash you if you're using Windows
54 if ($this->max !== null && $int > $this->max) {
55 return (string)$this->max;
57 return (string)$int;
60 /**
61 * @param string $string
62 * @return HTMLPurifier_AttrDef
64 public function make($string)
66 if ($string === '') {
67 $max = null;
68 } else {
69 $max = (int)$string;
71 $class = get_class($this);
72 return new $class($max);
76 // vim: et sw=4 sts=4