PSR-2 reformatting PHPDoc corrections
[htmlpurifier.git] / library / HTMLPurifier / AttrDef / CSS.php
blob02c1641fb2a14abb2165996d35eec77b1b9347e1
1 <?php
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.
14 class HTMLPurifier_AttrDef_CSS extends HTMLPurifier_AttrDef
17 /**
18 * @param string $css
19 * @param HTMLPurifier_Config $config
20 * @param HTMLPurifier_Context $context
21 * @return bool|string
23 public function validate($css, $config, $context)
25 $css = $this->parseCDATA($css);
27 $definition = $config->getCSSDefinition();
29 // we're going to break the spec and explode by semicolons.
30 // This is because semicolon rarely appears in escaped form
31 // Doing this is generally flaky but fast
32 // IT MIGHT APPEAR IN URIs, see HTMLPurifier_AttrDef_CSSURI
33 // for details
35 $declarations = explode(';', $css);
36 $propvalues = array();
38 /**
39 * Name of the current CSS property being validated.
41 $property = false;
42 $context->register('CurrentCSSProperty', $property);
44 foreach ($declarations as $declaration) {
45 if (!$declaration) {
46 continue;
48 if (!strpos($declaration, ':')) {
49 continue;
51 list($property, $value) = explode(':', $declaration, 2);
52 $property = trim($property);
53 $value = trim($value);
54 $ok = false;
55 do {
56 if (isset($definition->info[$property])) {
57 $ok = true;
58 break;
60 if (ctype_lower($property)) {
61 break;
63 $property = strtolower($property);
64 if (isset($definition->info[$property])) {
65 $ok = true;
66 break;
68 } while (0);
69 if (!$ok) {
70 continue;
72 // inefficient call, since the validator will do this again
73 if (strtolower(trim($value)) !== 'inherit') {
74 // inherit works for everything (but only on the base property)
75 $result = $definition->info[$property]->validate(
76 $value,
77 $config,
78 $context
80 } else {
81 $result = 'inherit';
83 if ($result === false) {
84 continue;
86 $propvalues[$property] = $result;
89 $context->destroy('CurrentCSSProperty');
91 // procedure does not write the new CSS simultaneously, so it's
92 // slightly inefficient, but it's the only way of getting rid of
93 // duplicates. Perhaps config to optimize it, but not now.
95 $new_declarations = '';
96 foreach ($propvalues as $prop => $value) {
97 $new_declarations .= "$prop:$value;";
100 return $new_declarations ? $new_declarations : false;
106 // vim: et sw=4 sts=4