1<?php
2
3/**
4 * Represents a Length as defined by CSS.
5 */
6class HTMLPurifier_AttrDef_CSS_Length extends HTMLPurifier_AttrDef
7{
8
9    /**
10     * @type HTMLPurifier_Length|string
11     */
12    protected $min;
13
14    /**
15     * @type HTMLPurifier_Length|string
16     */
17    protected $max;
18
19    /**
20     * @param HTMLPurifier_Length|string $min Minimum length, or null for no bound. String is also acceptable.
21     * @param HTMLPurifier_Length|string $max Maximum length, or null for no bound. String is also acceptable.
22     */
23    public function __construct($min = null, $max = null)
24    {
25        $this->min = $min !== null ? HTMLPurifier_Length::make($min) : null;
26        $this->max = $max !== null ? HTMLPurifier_Length::make($max) : null;
27    }
28
29    /**
30     * @param string $string
31     * @param HTMLPurifier_Config $config
32     * @param HTMLPurifier_Context $context
33     * @return bool|string
34     */
35    public function validate($string, $config, $context)
36    {
37        $string = $this->parseCDATA($string);
38
39        // Optimizations
40        if ($string === '') {
41            return false;
42        }
43        if ($string === '0') {
44            return '0';
45        }
46        if (strlen($string) === 1) {
47            return false;
48        }
49
50        $length = HTMLPurifier_Length::make($string);
51        if (!$length->isValid()) {
52            return false;
53        }
54
55        if ($this->min) {
56            $c = $length->compareTo($this->min);
57            if ($c === false) {
58                return false;
59            }
60            if ($c < 0) {
61                return false;
62            }
63        }
64        if ($this->max) {
65            $c = $length->compareTo($this->max);
66            if ($c === false) {
67                return false;
68            }
69            if ($c > 0) {
70                return false;
71            }
72        }
73        return $length->toString();
74    }
75}
76
77// vim: et sw=4 sts=4
78