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
14 class HTMLPurifier_AttrDef_CSS
extends HTMLPurifier_AttrDef
19 * @param HTMLPurifier_Config $config
20 * @param HTMLPurifier_Context $context
23 public function validate($css, $config, $context)
25 $css = $this->parseCDATA($css);
27 $definition = $config->getCSSDefinition();
28 $allow_duplicates = $config->get("CSS.AllowDuplicates");
31 // According to the CSS2.1 spec, the places where a
32 // non-delimiting semicolon can appear are in strings
33 // escape sequences. So here is some dumb hack to
37 $declarations = array();
39 for ($i = 0; $i < $len; $i++
) {
40 $c = strcspn($css, ";'\"", $i);
41 $accum .= substr($css, $i, $c);
43 if ($i == $len) break;
52 $declarations[] = $accum;
60 if ($accum != "") $declarations[] = $accum;
62 $propvalues = array();
63 $new_declarations = '';
66 * Name of the current CSS property being validated.
69 $context->register('CurrentCSSProperty', $property);
71 foreach ($declarations as $declaration) {
75 if (!strpos($declaration, ':')) {
78 list($property, $value) = explode(':', $declaration, 2);
79 $property = trim($property);
80 $value = trim($value);
83 if (isset($definition->info
[$property])) {
87 if (ctype_lower($property)) {
90 $property = strtolower($property);
91 if (isset($definition->info
[$property])) {
99 // inefficient call, since the validator will do this again
100 if (strtolower(trim($value)) !== 'inherit') {
101 // inherit works for everything (but only on the base property)
102 $result = $definition->info
[$property]->validate(
110 if ($result === false) {
113 if ($allow_duplicates) {
114 $new_declarations .= "$property:$result;";
116 $propvalues[$property] = $result;
120 $context->destroy('CurrentCSSProperty');
122 // procedure does not write the new CSS simultaneously, so it's
123 // slightly inefficient, but it's the only way of getting rid of
124 // duplicates. Perhaps config to optimize it, but not now.
126 foreach ($propvalues as $prop => $value) {
127 $new_declarations .= "$prop:$value;";
130 return $new_declarations ?
$new_declarations : false;
136 // vim: et sw=4 sts=4