3 require_once 'HTMLPurifier/ChildDef.php';
6 * Definition for tables
8 class HTMLPurifier_ChildDef_Table
extends HTMLPurifier_ChildDef
10 var $allow_empty = false;
12 var $elements = array('tr' => true, 'tbody' => true, 'thead' => true,
13 'tfoot' => true, 'caption' => true, 'colgroup' => true, 'col' => true);
14 function HTMLPurifier_ChildDef_Table() {}
15 function validateChildren($tokens_of_children, $config, &$context) {
16 if (empty($tokens_of_children)) return false;
18 // this ensures that the loop gets run one last time before closing
19 // up. It's a little bit of a hack, but it works! Just make sure you
20 // get rid of the token later.
21 $tokens_of_children[] = false;
23 // only one of these elements is allowed in a table
28 // as many of these as you want
32 $nesting = 0; // current depth so we can determine nodes
33 $is_collecting = false; // are we globbing together tokens to package
34 // into one of the collectors?
35 $collection = array(); // collected nodes
36 $tag_index = 0; // the first node might be whitespace,
37 // so this tells us where the start tag is
39 foreach ($tokens_of_children as $token) {
40 $is_child = ($nesting == 0);
42 if ($token === false) {
43 // terminating sequence started
44 } elseif ($token->type
== 'start') {
46 } elseif ($token->type
== 'end') {
50 // handle node collection
53 // okay, let's stash the tokens away
54 // first token tells us the type of the collection
55 switch ($collection[$tag_index]->name
) {
58 $content[] = $collection;
61 if ($caption !== false) break;
62 $caption = $collection;
66 // access the appropriate variable, $thead or $tfoot
67 $var = $collection[$tag_index]->name
;
68 if ($
$var === false) {
71 // transmutate the first and less entries into
72 // tbody tags, and then put into content
73 $collection[$tag_index]->name
= 'tbody';
74 $collection[count($collection)-1]->name
= 'tbody';
75 $content[] = $collection;
79 $cols[] = $collection;
82 $collection = array();
83 $is_collecting = false;
86 // add the node to the collection
87 $collection[] = $token;
92 if ($token === false) break;
95 // determine what we're dealing with
96 if ($token->name
== 'col') {
97 // the only empty tag in the possie, we can handle it
99 $cols[] = array_merge($collection, array($token));
100 $collection = array();
104 switch($token->name
) {
111 $is_collecting = true;
112 $collection[] = $token;
115 if ($token->type
== 'text' && $token->is_whitespace
) {
116 $collection[] = $token;
124 if (empty($content)) return false;
127 if ($caption !== false) $ret = array_merge($ret, $caption);
128 if ($cols !== false) foreach ($cols as $token_array) $ret = array_merge($ret, $token_array);
129 if ($thead !== false) $ret = array_merge($ret, $thead);
130 if ($tfoot !== false) $ret = array_merge($ret, $tfoot);
131 foreach ($content as $token_array) $ret = array_merge($ret, $token_array);
132 if (!empty($collection) && $is_collecting == false){
133 // grab the trailing space
134 $ret = array_merge($ret, $collection);
137 array_pop($tokens_of_children); // remove phantom token
139 return ($ret === $tokens_of_children) ?
true : $ret;