Deal with old libxml incompatibilities.
[htmlpurifier.git] / library / HTMLPurifier / AttrDef.php
blob739646fa7c26c4f4572d33b5af5d85a4be6b2db6
1 <?php
3 /**
4 * Base class for all validating attribute definitions.
6 * This family of classes forms the core for not only HTML attribute validation,
7 * but also any sort of string that needs to be validated or cleaned (which
8 * means CSS properties and composite definitions are defined here too).
9 * Besides defining (through code) what precisely makes the string valid,
10 * subclasses are also responsible for cleaning the code if possible.
13 abstract class HTMLPurifier_AttrDef
16 /**
17 * Tells us whether or not an HTML attribute is minimized.
18 * Has no meaning in other contexts.
19 * @type bool
21 public $minimized = false;
23 /**
24 * Tells us whether or not an HTML attribute is required.
25 * Has no meaning in other contexts
26 * @type bool
28 public $required = false;
30 /**
31 * Validates and cleans passed string according to a definition.
33 * @param string $string String to be validated and cleaned.
34 * @param HTMLPurifier_Config $config Mandatory HTMLPurifier_Config object.
35 * @param HTMLPurifier_Context $context Mandatory HTMLPurifier_Context object.
37 abstract public function validate($string, $config, $context);
39 /**
40 * Convenience method that parses a string as if it were CDATA.
42 * This method process a string in the manner specified at
43 * <http://www.w3.org/TR/html4/types.html#h-6.2> by removing
44 * leading and trailing whitespace, ignoring line feeds, and replacing
45 * carriage returns and tabs with spaces. While most useful for HTML
46 * attributes specified as CDATA, it can also be applied to most CSS
47 * values.
49 * @note This method is not entirely standards compliant, as trim() removes
50 * more types of whitespace than specified in the spec. In practice,
51 * this is rarely a problem, as those extra characters usually have
52 * already been removed by HTMLPurifier_Encoder.
54 * @warning This processing is inconsistent with XML's whitespace handling
55 * as specified by section 3.3.3 and referenced XHTML 1.0 section
56 * 4.7. However, note that we are NOT necessarily
57 * parsing XML, thus, this behavior may still be correct. We
58 * assume that newlines have been normalized.
60 public function parseCDATA($string)
62 $string = trim($string);
63 $string = str_replace(array("\n", "\t", "\r"), ' ', $string);
64 return $string;
67 /**
68 * Factory method for creating this class from a string.
69 * @param string $string String construction info
70 * @return HTMLPurifier_AttrDef Created AttrDef object corresponding to $string
72 public function make($string)
74 // default implementation, return a flyweight of this object.
75 // If $string has an effect on the returned object (i.e. you
76 // need to overload this method), it is best
77 // to clone or instantiate new copies. (Instantiation is safer.)
78 return $this;
81 /**
82 * Removes spaces from rgb(0, 0, 0) so that shorthand CSS properties work
83 * properly. THIS IS A HACK!
84 * @param string $string a CSS colour definition
85 * @return string
87 protected function mungeRgb($string)
89 $p = '\s*(\d+(\.\d+)?([%]?))\s*';
91 if (preg_match('/(rgba|hsla)\(/', $string)) {
92 return preg_replace('/(rgba|hsla)\('.$p.','.$p.','.$p.','.$p.'\)/', '\1(\2,\5,\8,\11)', $string);
95 return preg_replace('/(rgb|hsl)\('.$p.','.$p.','.$p.'\)/', '\1(\2,\5,\8)', $string);
98 /**
99 * Parses a possibly escaped CSS string and returns the "pure"
100 * version of it.
102 protected function expandCSSEscape($string)
104 // flexibly parse it
105 $ret = '';
106 for ($i = 0, $c = strlen($string); $i < $c; $i++) {
107 if ($string[$i] === '\\') {
108 $i++;
109 if ($i >= $c) {
110 $ret .= '\\';
111 break;
113 if (ctype_xdigit($string[$i])) {
114 $code = $string[$i];
115 for ($a = 1, $i++; $i < $c && $a < 6; $i++, $a++) {
116 if (!ctype_xdigit($string[$i])) {
117 break;
119 $code .= $string[$i];
121 // We have to be extremely careful when adding
122 // new characters, to make sure we're not breaking
123 // the encoding.
124 $char = HTMLPurifier_Encoder::unichr(hexdec($code));
125 if (HTMLPurifier_Encoder::cleanUTF8($char) === '') {
126 continue;
128 $ret .= $char;
129 if ($i < $c && trim($string[$i]) !== '') {
130 $i--;
132 continue;
134 if ($string[$i] === "\n") {
135 continue;
138 $ret .= $string[$i];
140 return $ret;
144 // vim: et sw=4 sts=4