Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / lib / htmlpurifier / HTMLPurifier / AttrDef / CSS.php
blob220ec0d0d1ce8bad3eb83922afe91bfbf85f583d
1 <?php
3 require_once 'HTMLPurifier/AttrDef.php';
4 require_once 'HTMLPurifier/CSSDefinition.php';
6 /**
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
15 * filtering solution.
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
30 // for details
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 );
47 } else {
48 $result = 'inherit';
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;