4 * Custom validation class, accepts DTD child definitions
6 * @warning Currently this class is an all or nothing proposition, that is,
7 * it will only give a bool return value.
9 class HTMLPurifier_ChildDef_Custom extends HTMLPurifier_ChildDef
14 public $type = 'custom';
19 public $allow_empty = false;
22 * Allowed child pattern as defined by the DTD.
28 * PCRE regex derived from $dtd_regex.
34 * @param $dtd_regex Allowed child pattern from the DTD
36 public function __construct($dtd_regex)
38 $this->dtd_regex = $dtd_regex;
39 $this->_compileRegex();
43 * Compiles the PCRE regex from a DTD regex ($dtd_regex to $_pcre_regex)
45 protected function _compileRegex()
47 $raw = str_replace(' ', '', $this->dtd_regex);
51 $el = '[#a-zA-Z0-9_.-]+';
54 // COMPLICATED! AND MIGHT BE BUGGY! I HAVE NO CLUE WHAT I'M
55 // DOING! Seriously: if there's problems, please report them.
57 // collect all elements into the $elements array
58 preg_match_all("/$el/", $reg, $matches);
59 foreach ($matches[0] as $match) {
60 $this->elements[$match] = true;
63 // setup all elements as parentheticals with leading commas
64 $reg = preg_replace("/$el/", '(,\\0)', $reg);
66 // remove commas when they were not solicited
67 $reg = preg_replace("/([^,(|]\(+),/", '\\1', $reg);
69 // remove all non-paranthetical commas: they are handled by first regex
70 $reg = preg_replace("/,\(/", '(', $reg);
72 $this->_pcre_regex = $reg;
76 * @param HTMLPurifier_Node[] $children
77 * @param HTMLPurifier_Config $config
78 * @param HTMLPurifier_Context $context
81 public function validateChildren($children, $config, $context)
83 $list_of_children = '';
84 $nesting = 0; // depth into the nest
85 foreach ($children as $node) {
86 if (!empty($node->is_whitespace)) {
89 $list_of_children .= $node->name . ',';
91 // add leading comma to deal with stray comma declarations
92 $list_of_children = ',' . rtrim($list_of_children, ',');
95 '/^,?' . $this->_pcre_regex . '$/',
102 // vim: et sw=4 sts=4