HTMLPurifier 4.4.0
|
00001 <?php 00002 00006 class HTMLPurifier_ChildDef_Required extends HTMLPurifier_ChildDef 00007 { 00012 public $elements = array(); 00016 protected $whitespace = false; 00020 public function __construct($elements) { 00021 if (is_string($elements)) { 00022 $elements = str_replace(' ', '', $elements); 00023 $elements = explode('|', $elements); 00024 } 00025 $keys = array_keys($elements); 00026 if ($keys == array_keys($keys)) { 00027 $elements = array_flip($elements); 00028 foreach ($elements as $i => $x) { 00029 $elements[$i] = true; 00030 if (empty($i)) unset($elements[$i]); // remove blank 00031 } 00032 } 00033 $this->elements = $elements; 00034 } 00035 public $allow_empty = false; 00036 public $type = 'required'; 00037 public function validateChildren($tokens_of_children, $config, $context) { 00038 // Flag for subclasses 00039 $this->whitespace = false; 00040 00041 // if there are no tokens, delete parent node 00042 if (empty($tokens_of_children)) return false; 00043 00044 // the new set of children 00045 $result = array(); 00046 00047 // current depth into the nest 00048 $nesting = 0; 00049 00050 // whether or not we're deleting a node 00051 $is_deleting = false; 00052 00053 // whether or not parsed character data is allowed 00054 // this controls whether or not we silently drop a tag 00055 // or generate escaped HTML from it 00056 $pcdata_allowed = isset($this->elements['#PCDATA']); 00057 00058 // a little sanity check to make sure it's not ALL whitespace 00059 $all_whitespace = true; 00060 00061 // some configuration 00062 $escape_invalid_children = $config->get('Core.EscapeInvalidChildren'); 00063 00064 // generator 00065 $gen = new HTMLPurifier_Generator($config, $context); 00066 00067 foreach ($tokens_of_children as $token) { 00068 if (!empty($token->is_whitespace)) { 00069 $result[] = $token; 00070 continue; 00071 } 00072 $all_whitespace = false; // phew, we're not talking about whitespace 00073 00074 $is_child = ($nesting == 0); 00075 00076 if ($token instanceof HTMLPurifier_Token_Start) { 00077 $nesting++; 00078 } elseif ($token instanceof HTMLPurifier_Token_End) { 00079 $nesting--; 00080 } 00081 00082 if ($is_child) { 00083 $is_deleting = false; 00084 if (!isset($this->elements[$token->name])) { 00085 $is_deleting = true; 00086 if ($pcdata_allowed && $token instanceof HTMLPurifier_Token_Text) { 00087 $result[] = $token; 00088 } elseif ($pcdata_allowed && $escape_invalid_children) { 00089 $result[] = new HTMLPurifier_Token_Text( 00090 $gen->generateFromToken($token) 00091 ); 00092 } 00093 continue; 00094 } 00095 } 00096 if (!$is_deleting || ($pcdata_allowed && $token instanceof HTMLPurifier_Token_Text)) { 00097 $result[] = $token; 00098 } elseif ($pcdata_allowed && $escape_invalid_children) { 00099 $result[] = 00100 new HTMLPurifier_Token_Text( 00101 $gen->generateFromToken($token) 00102 ); 00103 } else { 00104 // drop silently 00105 } 00106 } 00107 if (empty($result)) return false; 00108 if ($all_whitespace) { 00109 $this->whitespace = true; 00110 return false; 00111 } 00112 if ($tokens_of_children == $result) return true; 00113 return $result; 00114 } 00115 } 00116 00117 // vim: et sw=4 sts=4