00001 <?php 00002 00006 class HTMLPurifier_ChildDef_Table extends HTMLPurifier_ChildDef 00007 { 00008 public $allow_empty = false; 00009 public $type = 'table'; 00010 public $elements = array('tr' => true, 'tbody' => true, 'thead' => true, 00011 'tfoot' => true, 'caption' => true, 'colgroup' => true, 'col' => true); 00012 public function __construct() {} 00013 public function validateChildren($tokens_of_children, $config, $context) { 00014 if (empty($tokens_of_children)) return false; 00015 00016 // this ensures that the loop gets run one last time before closing 00017 // up. It's a little bit of a hack, but it works! Just make sure you 00018 // get rid of the token later. 00019 $tokens_of_children[] = false; 00020 00021 // only one of these elements is allowed in a table 00022 $caption = false; 00023 $thead = false; 00024 $tfoot = false; 00025 00026 // as many of these as you want 00027 $cols = array(); 00028 $content = array(); 00029 00030 $nesting = 0; // current depth so we can determine nodes 00031 $is_collecting = false; // are we globbing together tokens to package 00032 // into one of the collectors? 00033 $collection = array(); // collected nodes 00034 $tag_index = 0; // the first node might be whitespace, 00035 // so this tells us where the start tag is 00036 00037 foreach ($tokens_of_children as $token) { 00038 $is_child = ($nesting == 0); 00039 00040 if ($token === false) { 00041 // terminating sequence started 00042 } elseif ($token instanceof HTMLPurifier_Token_Start) { 00043 $nesting++; 00044 } elseif ($token instanceof HTMLPurifier_Token_End) { 00045 $nesting--; 00046 } 00047 00048 // handle node collection 00049 if ($is_collecting) { 00050 if ($is_child) { 00051 // okay, let's stash the tokens away 00052 // first token tells us the type of the collection 00053 switch ($collection[$tag_index]->name) { 00054 case 'tr': 00055 case 'tbody': 00056 $content[] = $collection; 00057 break; 00058 case 'caption': 00059 if ($caption !== false) break; 00060 $caption = $collection; 00061 break; 00062 case 'thead': 00063 case 'tfoot': 00064 // access the appropriate variable, $thead or $tfoot 00065 $var = $collection[$tag_index]->name; 00066 if ($$var === false) { 00067 $$var = $collection; 00068 } else { 00069 // transmutate the first and less entries into 00070 // tbody tags, and then put into content 00071 $collection[$tag_index]->name = 'tbody'; 00072 $collection[count($collection)-1]->name = 'tbody'; 00073 $content[] = $collection; 00074 } 00075 break; 00076 case 'colgroup': 00077 $cols[] = $collection; 00078 break; 00079 } 00080 $collection = array(); 00081 $is_collecting = false; 00082 $tag_index = 0; 00083 } else { 00084 // add the node to the collection 00085 $collection[] = $token; 00086 } 00087 } 00088 00089 // terminate 00090 if ($token === false) break; 00091 00092 if ($is_child) { 00093 // determine what we're dealing with 00094 if ($token->name == 'col') { 00095 // the only empty tag in the possie, we can handle it 00096 // immediately 00097 $cols[] = array_merge($collection, array($token)); 00098 $collection = array(); 00099 $tag_index = 0; 00100 continue; 00101 } 00102 switch($token->name) { 00103 case 'caption': 00104 case 'colgroup': 00105 case 'thead': 00106 case 'tfoot': 00107 case 'tbody': 00108 case 'tr': 00109 $is_collecting = true; 00110 $collection[] = $token; 00111 continue; 00112 default: 00113 if (!empty($token->is_whitespace)) { 00114 $collection[] = $token; 00115 $tag_index++; 00116 } 00117 continue; 00118 } 00119 } 00120 } 00121 00122 if (empty($content)) return false; 00123 00124 $ret = array(); 00125 if ($caption !== false) $ret = array_merge($ret, $caption); 00126 if ($cols !== false) foreach ($cols as $token_array) $ret = array_merge($ret, $token_array); 00127 if ($thead !== false) $ret = array_merge($ret, $thead); 00128 if ($tfoot !== false) $ret = array_merge($ret, $tfoot); 00129 foreach ($content as $token_array) $ret = array_merge($ret, $token_array); 00130 if (!empty($collection) && $is_collecting == false){ 00131 // grab the trailing space 00132 $ret = array_merge($ret, $collection); 00133 } 00134 00135 array_pop($tokens_of_children); // remove phantom token 00136 00137 return ($ret === $tokens_of_children) ? true : $ret; 00138 00139 } 00140 } 00141 00142 // vim: et sw=4 sts=4
| Copyright © 2003 - 2009 MyOOS [Shopsystem]. All rights reserved. MyOOS [Shopsystem] is Free Software released under the GNU/GPL License. Webmaster: info@r23.de (Impressum) |
|
