Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / lib / htmlpurifier / HTMLPurifier / ChildDef / Custom.php
blobde18cd70701b7f6dcff85ba4282dc5b6576e5533
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 var $type = 'custom';
16 var $allow_empty = false;
17 /**
18 * Allowed child pattern as defined by the DTD
20 var $dtd_regex;
21 /**
22 * PCRE regex derived from $dtd_regex
23 * @private
25 var $_pcre_regex;
26 /**
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();
33 /**
34 * Compiles the PCRE regex from a DTD regex ($dtd_regex to $_pcre_regex)
36 function _compileRegex() {
37 $raw = str_replace(' ', '', $this->dtd_regex);
38 if ($raw{0} != '(') {
39 $raw = "($raw)";
41 $reg = str_replace(',', ',?', $raw);
42 $reg = preg_replace('/([#a-zA-Z0-9_.-]+)/', '(,?\\0)', $reg);
43 $this->_pcre_regex = $reg;
45 function validateChildren($tokens_of_children, $config, &$context) {
46 $list_of_children = '';
47 $nesting = 0; // depth into the nest
48 foreach ($tokens_of_children as $token) {
49 if (!empty($token->is_whitespace)) continue;
51 $is_child = ($nesting == 0); // direct
53 if ($token->type == 'start') {
54 $nesting++;
55 } elseif ($token->type == 'end') {
56 $nesting--;
59 if ($is_child) {
60 $list_of_children .= $token->name . ',';
63 $list_of_children = rtrim($list_of_children, ',');
65 $okay =
66 preg_match(
67 '/^'.$this->_pcre_regex.'$/',
68 $list_of_children
71 return (bool) $okay;