[3.0.0] Convert all $context calls away from references
[htmlpurifier.git] / library / HTMLPurifier / AttrDef / CSS / Multiple.php
blob3488f0b6a72bf27e19e7c82f1473d9613af92400
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.
21 * @todo Make protected
23 public $single;
25 /**
26 * Max number of values allowed.
27 * @todo Make protected
29 public $max;
31 /**
32 * @param $single HTMLPurifier_AttrDef to multiply
33 * @param $max Max number of values allowed (usually four)
35 public function __construct($single, $max = 4) {
36 $this->single = $single;
37 $this->max = $max;
40 public function validate($string, $config, $context) {
41 $string = $this->parseCDATA($string);
42 if ($string === '') return false;
43 $parts = explode(' ', $string); // parseCDATA replaced \r, \t and \n
44 $length = count($parts);
45 $final = '';
46 for ($i = 0, $num = 0; $i < $length && $num < $this->max; $i++) {
47 if (ctype_space($parts[$i])) continue;
48 $result = $this->single->validate($parts[$i], $config, $context);
49 if ($result !== false) {
50 $final .= $result . ' ';
51 $num++;
54 if ($final === '') return false;
55 return rtrim($final);