[3.1.0] Convert tokens to use instanceof, reducing memory footprint and improving...
[htmlpurifier/bfroehle.git] / library / HTMLPurifier / ChildDef / Custom.php
blob5f5210fe89db65172253696213c834c57cd2362e
1 <?php
3 require_once 'HTMLPurifier/ChildDef.php';
5 /**
6 * Custom validation class, accepts DTD child definitions
7 *
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
11 * tested.
13 class HTMLPurifier_ChildDef_Custom extends HTMLPurifier_ChildDef
15 public $type = 'custom';
16 public $allow_empty = false;
17 /**
18 * Allowed child pattern as defined by the DTD
20 public $dtd_regex;
21 /**
22 * PCRE regex derived from $dtd_regex
23 * @private
25 private $_pcre_regex;
26 /**
27 * @param $dtd_regex Allowed child pattern from the DTD
29 public function __construct($dtd_regex) {
30 $this->dtd_regex = $dtd_regex;
31 $this->_compileRegex();
33 /**
34 * Compiles the PCRE regex from a DTD regex ($dtd_regex to $_pcre_regex)
36 protected function _compileRegex() {
37 $raw = str_replace(' ', '', $this->dtd_regex);
38 if ($raw{0} != '(') {
39 $raw = "($raw)";
41 $el = '[#a-zA-Z0-9_.-]+';
42 $reg = $raw;
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 public 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 instanceof HTMLPurifier_Token_Start) {
73 $nesting++;
74 } elseif ($token instanceof HTMLPurifier_Token_End) {
75 $nesting--;
78 if ($is_child) {
79 $list_of_children .= $token->name . ',';
82 // add leading comma to deal with stray comma declarations
83 $list_of_children = ',' . rtrim($list_of_children, ',');
84 $okay =
85 preg_match(
86 '/^,?'.$this->_pcre_regex.'$/',
87 $list_of_children
90 return (bool) $okay;