1<?php
2
3/**
4 * Validates a MultiLength as defined by the HTML spec.
5 *
6 * A multilength is either a integer (pixel count), a percentage, or
7 * a relative number.
8 */
9class HTMLPurifier_AttrDef_HTML_MultiLength extends HTMLPurifier_AttrDef_HTML_Length
10{
11
12    /**
13     * @param string $string
14     * @param HTMLPurifier_Config $config
15     * @param HTMLPurifier_Context $context
16     * @return bool|string
17     */
18    public function validate($string, $config, $context)
19    {
20        $string = trim($string);
21        if ($string === '') {
22            return false;
23        }
24
25        $parent_result = parent::validate($string, $config, $context);
26        if ($parent_result !== false) {
27            return $parent_result;
28        }
29
30        $length = strlen($string);
31        $last_char = $string[$length - 1];
32
33        if ($last_char !== '*') {
34            return false;
35        }
36
37        $int = substr($string, 0, $length - 1);
38
39        if ($int == '') {
40            return '*';
41        }
42        if (!is_numeric($int)) {
43            return false;
44        }
45
46        $int = (int)$int;
47        if ($int < 0) {
48            return false;
49        }
50        if ($int == 0) {
51            return '0';
52        }
53        if ($int == 1) {
54            return '*';
55        }
56        return ((string)$int) . '*';
57    }
58}
59
60// vim: et sw=4 sts=4
61