Release 2.0.1, merged in 1181 to HEAD.
[htmlpurifier.git] / library / HTMLPurifier / AttrDef / CSS / Multiple.php
blob9a818d108af1008427b509e0dee762ce2cab81a1
1 <?php
3 require_once 'HTMLPurifier/AttrDef.php';
5 /**
6 * Framework class for strings that involve multiple values.
7 *
8 * Certain CSS properties such as border-width and margin allow multiple
9 * lengths to be specified. This class can take a vanilla border-width
10 * definition and multiply it, usually into a max of four.
12 * @note Even though the CSS specification isn't clear about it, inherit
13 * can only be used alone: it will never manifest as part of a multi
14 * shorthand declaration. Thus, this class does not allow inherit.
16 class HTMLPurifier_AttrDef_CSS_Multiple extends HTMLPurifier_AttrDef
19 /**
20 * Instance of component definition to defer validation to.
22 var $single;
24 /**
25 * Max number of values allowed.
27 var $max;
29 /**
30 * @param $single HTMLPurifier_AttrDef to multiply
31 * @param $max Max number of values allowed (usually four)
33 function HTMLPurifier_AttrDef_CSS_Multiple($single, $max = 4) {
34 $this->single = $single;
35 $this->max = $max;
38 function validate($string, $config, &$context) {
39 $string = $this->parseCDATA($string);
40 if ($string === '') return false;
41 $parts = explode(' ', $string); // parseCDATA replaced \r, \t and \n
42 $length = count($parts);
43 $final = '';
44 for ($i = 0, $num = 0; $i < $length && $num < $this->max; $i++) {
45 if (ctype_space($parts[$i])) continue;
46 $result = $this->single->validate($parts[$i], $config, $context);
47 if ($result !== false) {
48 $final .= $result . ' ';
49 $num++;
52 if ($final === '') return false;
53 return rtrim($final);