4 * Definition for tables
6 class HTMLPurifier_ChildDef_Table
extends HTMLPurifier_ChildDef
8 public $allow_empty = false;
9 public $type = 'table';
10 public $elements = array('tr' => true, 'tbody' => true, 'thead' => true,
11 'tfoot' => true, 'caption' => true, 'colgroup' => true, 'col' => true);
12 public function __construct() {}
13 public function validateChildren($tokens_of_children, $config, $context) {
14 if (empty($tokens_of_children)) return false;
16 // this ensures that the loop gets run one last time before closing
17 // up. It's a little bit of a hack, but it works! Just make sure you
18 // get rid of the token later.
19 $tokens_of_children[] = false;
21 // only one of these elements is allowed in a table
26 // as many of these as you want
30 $nesting = 0; // current depth so we can determine nodes
31 $is_collecting = false; // are we globbing together tokens to package
32 // into one of the collectors?
33 $collection = array(); // collected nodes
34 $tag_index = 0; // the first node might be whitespace,
35 // so this tells us where the start tag is
37 foreach ($tokens_of_children as $token) {
38 $is_child = ($nesting == 0);
40 if ($token === false) {
41 // terminating sequence started
42 } elseif ($token instanceof HTMLPurifier_Token_Start
) {
44 } elseif ($token instanceof HTMLPurifier_Token_End
) {
48 // handle node collection
51 // okay, let's stash the tokens away
52 // first token tells us the type of the collection
53 switch ($collection[$tag_index]->name
) {
56 $content[] = $collection;
59 if ($caption !== false) break;
60 $caption = $collection;
64 // access the appropriate variable, $thead or $tfoot
65 $var = $collection[$tag_index]->name
;
66 if ($
$var === false) {
69 // transmutate the first and less entries into
70 // tbody tags, and then put into content
71 $collection[$tag_index]->name
= 'tbody';
72 $collection[count($collection)-1]->name
= 'tbody';
73 $content[] = $collection;
77 $cols[] = $collection;
80 $collection = array();
81 $is_collecting = false;
84 // add the node to the collection
85 $collection[] = $token;
90 if ($token === false) break;
93 // determine what we're dealing with
94 if ($token->name
== 'col') {
95 // the only empty tag in the possie, we can handle it
97 $cols[] = array_merge($collection, array($token));
98 $collection = array();
102 switch($token->name
) {
109 $is_collecting = true;
110 $collection[] = $token;
113 if (!empty($token->is_whitespace
)) {
114 $collection[] = $token;
122 if (empty($content)) return false;
125 if ($caption !== false) $ret = array_merge($ret, $caption);
126 if ($cols !== false) foreach ($cols as $token_array) $ret = array_merge($ret, $token_array);
127 if ($thead !== false) $ret = array_merge($ret, $thead);
128 if ($tfoot !== false) $ret = array_merge($ret, $tfoot);
129 foreach ($content as $token_array) $ret = array_merge($ret, $token_array);
130 if (!empty($collection) && $is_collecting == false){
131 // grab the trailing space
132 $ret = array_merge($ret, $collection);
135 array_pop($tokens_of_children); // remove phantom token
137 return ($ret === $tokens_of_children) ?
true : $ret;
142 // vim: et sw=4 sts=4