Completed
Push — master ( 14e79d...7a4940 )
by Richard
08:21
created

HTMLPurifier_AttrDef_CSS::validate()   D

Complexity

Conditions 14
Paths 124

Size

Total Lines 85
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 48
nc 124
nop 3
dl 0
loc 85
rs 4.6283
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Validates the HTML attribute style, otherwise known as CSS.
5
 * @note We don't implement the whole CSS specification, so it might be
6
 *       difficult to reuse this component in the context of validating
7
 *       actual stylesheet declarations.
8
 * @note If we were really serious about validating the CSS, we would
9
 *       tokenize the styles and then parse the tokens. Obviously, we
10
 *       are not doing that. Doing that could seriously harm performance,
11
 *       but would make these components a lot more viable for a CSS
12
 *       filtering solution.
13
 */
14
class HTMLPurifier_AttrDef_CSS extends HTMLPurifier_AttrDef
15
{
16
17
    /**
18
     * @param string $css
19
     * @param HTMLPurifier_Config $config
20
     * @param HTMLPurifier_Context $context
21
     * @return bool|string
22
     */
23
    public function validate($css, $config, $context)
24
    {
25
        $css = $this->parseCDATA($css);
26
27
        $definition = $config->getCSSDefinition();
28
        $allow_duplicates = $config->get("CSS.AllowDuplicates");
29
30
        // we're going to break the spec and explode by semicolons.
31
        // This is because semicolon rarely appears in escaped form
32
        // Doing this is generally flaky but fast
33
        // IT MIGHT APPEAR IN URIs, see HTMLPurifier_AttrDef_CSSURI
34
        // for details
35
36
        $declarations = explode(';', $css);
37
        $propvalues = array();
38
        $new_declarations = '';
39
40
        /**
41
         * Name of the current CSS property being validated.
42
         */
43
        $property = false;
44
        $context->register('CurrentCSSProperty', $property);
45
46
        foreach ($declarations as $declaration) {
47
            if (!$declaration) {
48
                continue;
49
            }
50
            if (!strpos($declaration, ':')) {
51
                continue;
52
            }
53
            list($property, $value) = explode(':', $declaration, 2);
54
            $property = trim($property);
55
            $value = trim($value);
56
            $ok = false;
57
            do {
58
                if (isset($definition->info[$property])) {
59
                    $ok = true;
60
                    break;
61
                }
62
                if (ctype_lower($property)) {
63
                    break;
64
                }
65
                $property = strtolower($property);
66
                if (isset($definition->info[$property])) {
67
                    $ok = true;
68
                    break;
69
                }
70
            } while (0);
71
            if (!$ok) {
72
                continue;
73
            }
74
            // inefficient call, since the validator will do this again
75
            if (strtolower(trim($value)) !== 'inherit') {
76
                // inherit works for everything (but only on the base property)
77
                $result = $definition->info[$property]->validate(
78
                    $value,
79
                    $config,
80
                    $context
81
                );
82
            } else {
83
                $result = 'inherit';
84
            }
85
            if ($result === false) {
86
                continue;
87
            }
88
            if ($allow_duplicates) {
89
                $new_declarations .= "$property:$result;";
90
            } else {
91
                $propvalues[$property] = $result;
92
            }
93
        }
94
95
        $context->destroy('CurrentCSSProperty');
96
97
        // procedure does not write the new CSS simultaneously, so it's
98
        // slightly inefficient, but it's the only way of getting rid of
99
        // duplicates. Perhaps config to optimize it, but not now.
100
101
        foreach ($propvalues as $prop => $value) {
102
            $new_declarations .= "$prop:$value;";
103
        }
104
105
        return $new_declarations ? $new_declarations : false;
106
107
    }
108
109
}
110
111
// vim: et sw=4 sts=4
112