3 require_once 'HTMLPurifier/ChildDef.php';
6 * Custom validation class, accepts DTD child definitions
8 * @warning Currently this class is an all or nothing proposition, that is,
9 * it will only give a bool return value.
10 * @note This class is currently not used by any code, although it is unit
13 class HTMLPurifier_ChildDef_Custom
extends HTMLPurifier_ChildDef
16 var $allow_empty = false;
18 * Allowed child pattern as defined by the DTD
22 * PCRE regex derived from $dtd_regex
27 * @param $dtd_regex Allowed child pattern from the DTD
29 function HTMLPurifier_ChildDef_Custom($dtd_regex) {
30 $this->dtd_regex
= $dtd_regex;
31 $this->_compileRegex();
34 * Compiles the PCRE regex from a DTD regex ($dtd_regex to $_pcre_regex)
36 function _compileRegex() {
37 $raw = str_replace(' ', '', $this->dtd_regex
);
41 $el = '[#a-zA-Z0-9_.-]+';
44 // COMPLICATED! AND MIGHT BE BUGGY! I HAVE NO CLUE WHAT I'M
45 // DOING! Seriously: if there's problems, please report them.
47 // collect all elements into the $elements array
48 preg_match_all("/$el/", $reg, $matches);
49 foreach ($matches[0] as $match) {
50 $this->elements
[$match] = true;
53 // setup all elements as parentheticals with leading commas
54 $reg = preg_replace("/$el/", '(,\\0)', $reg);
56 // remove commas when they were not solicited
57 $reg = preg_replace("/([^,(|]\(+),/", '\\1', $reg);
59 // remove all non-paranthetical commas: they are handled by first regex
60 $reg = preg_replace("/,\(/", '(', $reg);
62 $this->_pcre_regex
= $reg;
64 function validateChildren($tokens_of_children, $config, &$context) {
65 $list_of_children = '';
66 $nesting = 0; // depth into the nest
67 foreach ($tokens_of_children as $token) {
68 if (!empty($token->is_whitespace
)) continue;
70 $is_child = ($nesting == 0); // direct
72 if ($token->type
== 'start') {
74 } elseif ($token->type
== 'end') {
79 $list_of_children .= $token->name
. ',';
82 // add leading comma to deal with stray comma declarations
83 $list_of_children = ',' . rtrim($list_of_children, ',');
86 '/^,?'.$this->_pcre_regex
.'$/',