3 require_once 'HTMLPurifier/AttrDef.php';
4 require_once 'HTMLPurifier/CSSDefinition.php';
7 * Validates the HTML attribute style, otherwise known as CSS.
8 * @note We don't implement the whole CSS specification, so it might be
9 * difficult to reuse this component in the context of validating
10 * actual stylesheet declarations.
11 * @note If we were really serious about validating the CSS, we would
12 * tokenize the styles and then parse the tokens. Obviously, we
13 * are not doing that. Doing that could seriously harm performance,
14 * but would make these components a lot more viable for a CSS
17 class HTMLPurifier_AttrDef_CSS
extends HTMLPurifier_AttrDef
20 function validate($css, $config, &$context) {
22 $css = $this->parseCDATA($css);
24 $definition = $config->getCSSDefinition();
26 // we're going to break the spec and explode by semicolons.
27 // This is because semicolon rarely appears in escaped form
28 // Doing this is generally flaky but fast
29 // IT MIGHT APPEAR IN URIs, see HTMLPurifier_AttrDef_CSSURI
32 $declarations = explode(';', $css);
33 $propvalues = array();
35 foreach ($declarations as $declaration) {
36 if (!$declaration) continue;
37 if (!strpos($declaration, ':')) continue;
38 list($property, $value) = explode(':', $declaration, 2);
39 $property = trim($property);
40 $value = trim($value);
41 if (!isset($definition->info
[$property])) continue;
42 // inefficient call, since the validator will do this again
43 if (strtolower(trim($value)) !== 'inherit') {
44 // inherit works for everything (but only on the base property)
45 $result = $definition->info
[$property]->validate(
46 $value, $config, $context );
50 if ($result === false) continue;
51 $propvalues[$property] = $result;
54 // procedure does not write the new CSS simultaneously, so it's
55 // slightly inefficient, but it's the only way of getting rid of
56 // duplicates. Perhaps config to optimize it, but not now.
58 $new_declarations = '';
59 foreach ($propvalues as $prop => $value) {
60 $new_declarations .= "$prop:$value;";
63 return $new_declarations ?
$new_declarations : false;