~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to lib/htmlpurifier/HTMLPurifier/AttrDef/CSS/Number.php

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
 
3
 
/**
4
 
 * Validates a number as defined by the CSS spec.
5
 
 */
6
 
class HTMLPurifier_AttrDef_CSS_Number extends HTMLPurifier_AttrDef
7
 
{
8
 
 
9
 
    /**
10
 
     * Bool indicating whether or not only positive values allowed.
11
 
     */
12
 
    protected $non_negative = false;
13
 
 
14
 
    /**
15
 
     * @param $non_negative Bool indicating whether negatives are forbidden
16
 
     */
17
 
    public function __construct($non_negative = false) {
18
 
        $this->non_negative = $non_negative;
19
 
    }
20
 
 
21
 
    /**
22
 
     * @warning Some contexts do not pass $config, $context. These
23
 
     *          variables should not be used without checking HTMLPurifier_Length
24
 
     */
25
 
    public function validate($number, $config, $context) {
26
 
 
27
 
        $number = $this->parseCDATA($number);
28
 
 
29
 
        if ($number === '') return false;
30
 
        if ($number === '0') return '0';
31
 
 
32
 
        $sign = '';
33
 
        switch ($number[0]) {
34
 
            case '-':
35
 
                if ($this->non_negative) return false;
36
 
                $sign = '-';
37
 
            case '+':
38
 
                $number = substr($number, 1);
39
 
        }
40
 
 
41
 
        if (ctype_digit($number)) {
42
 
            $number = ltrim($number, '0');
43
 
            return $number ? $sign . $number : '0';
44
 
        }
45
 
 
46
 
        // Period is the only non-numeric character allowed
47
 
        if (strpos($number, '.') === false) return false;
48
 
 
49
 
        list($left, $right) = explode('.', $number, 2);
50
 
 
51
 
        if ($left === '' && $right === '') return false;
52
 
        if ($left !== '' && !ctype_digit($left)) return false;
53
 
 
54
 
        $left  = ltrim($left,  '0');
55
 
        $right = rtrim($right, '0');
56
 
 
57
 
        if ($right === '') {
58
 
            return $left ? $sign . $left : '0';
59
 
        } elseif (!ctype_digit($right)) {
60
 
            return false;
61
 
        }
62
 
 
63
 
        return $sign . $left . '.' . $right;
64
 
 
65
 
    }
66
 
 
67
 
}
68
 
 
69
 
// vim: et sw=4 sts=4
 
1
<?php
 
2
 
 
3
/**
 
4
 * Validates a number as defined by the CSS spec.
 
5
 */
 
6
class HTMLPurifier_AttrDef_CSS_Number extends HTMLPurifier_AttrDef
 
7
{
 
8
 
 
9
    /**
 
10
     * Indicates whether or not only positive values are allowed.
 
11
     * @type bool
 
12
     */
 
13
    protected $non_negative = false;
 
14
 
 
15
    /**
 
16
     * @param bool $non_negative indicates whether negatives are forbidden
 
17
     */
 
18
    public function __construct($non_negative = false)
 
19
    {
 
20
        $this->non_negative = $non_negative;
 
21
    }
 
22
 
 
23
    /**
 
24
     * @param string $number
 
25
     * @param HTMLPurifier_Config $config
 
26
     * @param HTMLPurifier_Context $context
 
27
     * @return string|bool
 
28
     * @warning Some contexts do not pass $config, $context. These
 
29
     *          variables should not be used without checking HTMLPurifier_Length
 
30
     */
 
31
    public function validate($number, $config, $context)
 
32
    {
 
33
        $number = $this->parseCDATA($number);
 
34
 
 
35
        if ($number === '') {
 
36
            return false;
 
37
        }
 
38
        if ($number === '0') {
 
39
            return '0';
 
40
        }
 
41
 
 
42
        $sign = '';
 
43
        switch ($number[0]) {
 
44
            case '-':
 
45
                if ($this->non_negative) {
 
46
                    return false;
 
47
                }
 
48
                $sign = '-';
 
49
            case '+':
 
50
                $number = substr($number, 1);
 
51
        }
 
52
 
 
53
        if (ctype_digit($number)) {
 
54
            $number = ltrim($number, '0');
 
55
            return $number ? $sign . $number : '0';
 
56
        }
 
57
 
 
58
        // Period is the only non-numeric character allowed
 
59
        if (strpos($number, '.') === false) {
 
60
            return false;
 
61
        }
 
62
 
 
63
        list($left, $right) = explode('.', $number, 2);
 
64
 
 
65
        if ($left === '' && $right === '') {
 
66
            return false;
 
67
        }
 
68
        if ($left !== '' && !ctype_digit($left)) {
 
69
            return false;
 
70
        }
 
71
 
 
72
        $left = ltrim($left, '0');
 
73
        $right = rtrim($right, '0');
 
74
 
 
75
        if ($right === '') {
 
76
            return $left ? $sign . $left : '0';
 
77
        } elseif (!ctype_digit($right)) {
 
78
            return false;
 
79
        }
 
80
        return $sign . $left . '.' . $right;
 
81
    }
 
82
}
 
83
 
 
84
// vim: et sw=4 sts=4