3 require_once 'HTMLPurifier/AttrDef.php';
7 * Validates a keyword against a list of valid values.
8 * @warning The case-insensitive compare of this function uses PHP's
9 * built-in strtolower and ctype_lower functions, which may
10 * cause problems with international comparisons
12 class HTMLPurifier_AttrDef_Enum
extends HTMLPurifier_AttrDef
16 * Lookup table of valid values.
18 var $valid_values = array();
21 * Bool indicating whether or not enumeration is case sensitive.
22 * @note In general this is always case insensitive.
24 var $case_sensitive = false; // values according to W3C spec
27 * @param $valid_values List of valid values
28 * @param $case_sensitive Bool indicating whether or not case sensitive
30 function HTMLPurifier_AttrDef_Enum(
31 $valid_values = array(), $case_sensitive = false
33 $this->valid_values
= array_flip($valid_values);
34 $this->case_sensitive
= $case_sensitive;
37 function validate($string, $config, &$context) {
38 $string = trim($string);
39 if (!$this->case_sensitive
) {
40 // we may want to do full case-insensitive libraries
41 $string = ctype_lower($string) ?
$string : strtolower($string);
43 $result = isset($this->valid_values
[$string]);
45 return $result ?
$string : false;
49 * @param $string In form of comma-delimited list of case-insensitive
50 * valid values. Example: "foo,bar,baz". Prepend "s:" to make
53 function make($string) {
54 if (strlen($string) > 2 && $string[0] == 's' && $string[1] == ':') {
55 $string = substr($string, 2);
60 $values = explode(',', $string);
61 return new HTMLPurifier_AttrDef_Enum($values, $sensitive);