library/HTMLPurifier/ChildDef/Custom.php

Go to the documentation of this file.
00001 <?php
00002 
00011 class HTMLPurifier_ChildDef_Custom extends HTMLPurifier_ChildDef
00012 {
00013     public $type = 'custom';
00014     public $allow_empty = false;
00018     public $dtd_regex;
00023     private $_pcre_regex;
00027     public function __construct($dtd_regex) {
00028         $this->dtd_regex = $dtd_regex;
00029         $this->_compileRegex();
00030     }
00034     protected function _compileRegex() {
00035         $raw = str_replace(' ', '', $this->dtd_regex);
00036         if ($raw{0} != '(') {
00037             $raw = "($raw)";
00038         }
00039         $el = '[#a-zA-Z0-9_.-]+';
00040         $reg = $raw;
00041         
00042         // COMPLICATED! AND MIGHT BE BUGGY! I HAVE NO CLUE WHAT I'M
00043         // DOING! Seriously: if there's problems, please report them.
00044         
00045         // collect all elements into the $elements array
00046         preg_match_all("/$el/", $reg, $matches);
00047         foreach ($matches[0] as $match) {
00048             $this->elements[$match] = true;
00049         }
00050         
00051         // setup all elements as parentheticals with leading commas
00052         $reg = preg_replace("/$el/", '(,\\0)', $reg);
00053         
00054         // remove commas when they were not solicited
00055         $reg = preg_replace("/([^,(|]\(+),/", '\\1', $reg);
00056         
00057         // remove all non-paranthetical commas: they are handled by first regex
00058         $reg = preg_replace("/,\(/", '(', $reg);
00059         
00060         $this->_pcre_regex = $reg;
00061     }
00062     public function validateChildren($tokens_of_children, $config, $context) {
00063         $list_of_children = '';
00064         $nesting = 0; // depth into the nest
00065         foreach ($tokens_of_children as $token) {
00066             if (!empty($token->is_whitespace)) continue;
00067             
00068             $is_child = ($nesting == 0); // direct
00069             
00070             if ($token instanceof HTMLPurifier_Token_Start) {
00071                 $nesting++;
00072             } elseif ($token instanceof HTMLPurifier_Token_End) {
00073                 $nesting--;
00074             }
00075             
00076             if ($is_child) {
00077                 $list_of_children .= $token->name . ',';
00078             }
00079         }
00080         // add leading comma to deal with stray comma declarations
00081         $list_of_children = ',' . rtrim($list_of_children, ',');
00082         $okay =
00083             preg_match(
00084                 '/^,?'.$this->_pcre_regex.'$/',
00085                 $list_of_children
00086             );
00087         
00088         return (bool) $okay;
00089     }
00090 }
00091 

Generated on Thu Jun 19 18:47:26 2008 for HTMLPurifier by  doxygen 1.5.3