Source for file Table.php

Documentation is available at Table.php

  1. <?php
  2.  
  3. /**
  4.  * Definition for tables
  5.  */
  6. {
  7.     public $allow_empty = false;
  8.     public $type = 'table';
  9.     public $elements = array('tr' => true'tbody' => true'thead' => true,
  10.         'tfoot' => true'caption' => true'colgroup' => true'col' => true);
  11.     public function __construct({}
  12.     public function validateChildren($tokens_of_children$config$context{
  13.         if (empty($tokens_of_children)) return false;
  14.         
  15.         // this ensures that the loop gets run one last time before closing
  16.         // up. It's a little bit of a hack, but it works! Just make sure you
  17.         // get rid of the token later.
  18.         $tokens_of_children[false;
  19.         
  20.         // only one of these elements is allowed in a table
  21.         $caption false;
  22.         $thead   false;
  23.         $tfoot   false;
  24.         
  25.         // as many of these as you want
  26.         $cols    array();
  27.         $content array();
  28.         
  29.         $nesting 0// current depth so we can determine nodes
  30.         $is_collecting false// are we globbing together tokens to package
  31.                                 // into one of the collectors?
  32.         $collection array()// collected nodes
  33.         $tag_index 0// the first node might be whitespace,
  34.                             // so this tells us where the start tag is
  35.         
  36.         foreach ($tokens_of_children as $token{
  37.             $is_child ($nesting == 0);
  38.             
  39.             if ($token === false{
  40.                 // terminating sequence started
  41.             elseif ($token instanceof HTMLPurifier_Token_Start{
  42.                 $nesting++;
  43.             elseif ($token instanceof HTMLPurifier_Token_End{
  44.                 $nesting--;
  45.             }
  46.             
  47.             // handle node collection
  48.             if ($is_collecting{
  49.                 if ($is_child{
  50.                     // okay, let's stash the tokens away
  51.                     // first token tells us the type of the collection
  52.                     switch ($collection[$tag_index]->name{
  53.                         case 'tr':
  54.                         case 'tbody':
  55.                             $content[$collection;
  56.                             break;
  57.                         case 'caption':
  58.                             if ($caption !== falsebreak;
  59.                             $caption $collection;
  60.                             break;
  61.                         case 'thead':
  62.                         case 'tfoot':
  63.                             // access the appropriate variable, $thead or $tfoot
  64.                             $var $collection[$tag_index]->name;
  65.                             if ($$var === false{
  66.                                 $$var $collection;
  67.                             else {
  68.                                 // transmutate the first and less entries into
  69.                                 // tbody tags, and then put into content
  70.                                 $collection[$tag_index]->name 'tbody';
  71.                                 $collection[count($collection)-1]->name 'tbody';
  72.                                 $content[$collection;
  73.                             }
  74.                             break;
  75.                          case 'colgroup':
  76.                             $cols[$collection;
  77.                             break;
  78.                     }
  79.                     $collection array();
  80.                     $is_collecting false;
  81.                     $tag_index 0;
  82.                 else {
  83.                     // add the node to the collection
  84.                     $collection[$token;
  85.                 }
  86.             }
  87.             
  88.             // terminate
  89.             if ($token === falsebreak;
  90.             
  91.             if ($is_child{
  92.                 // determine what we're dealing with
  93.                 if ($token->name == 'col'{
  94.                     // the only empty tag in the possie, we can handle it
  95.                     // immediately
  96.                     $cols[array_merge($collectionarray($token));
  97.                     $collection array();
  98.                     $tag_index 0;
  99.                     continue;
  100.                 }
  101.                 switch($token->name{
  102.                     case 'caption':
  103.                     case 'colgroup':
  104.                     case 'thead':
  105.                     case 'tfoot':
  106.                     case 'tbody':
  107.                     case 'tr':
  108.                         $is_collecting true;
  109.                         $collection[$token;
  110.                         continue;
  111.                     default:
  112.                         if ($token instanceof HTMLPurifier_Token_Text && $token->is_whitespace{
  113.                             $collection[$token;
  114.                             $tag_index++;
  115.                         }
  116.                         continue;
  117.                 }
  118.             }
  119.         }
  120.         
  121.         if (empty($content)) return false;
  122.         
  123.         $ret array();
  124.         if ($caption !== false$ret array_merge($ret$caption);
  125.         if ($cols !== false)    foreach ($cols as $token_array$ret array_merge($ret$token_array);
  126.         if ($thead !== false)   $ret array_merge($ret$thead);
  127.         if ($tfoot !== false)   $ret array_merge($ret$tfoot);
  128.         foreach ($content as $token_array$ret array_merge($ret$token_array);
  129.         if (!empty($collection&& $is_collecting == false){
  130.             // grab the trailing space
  131.             $ret array_merge($ret$collection);
  132.         }
  133.         
  134.         array_pop($tokens_of_children)// remove phantom token
  135.         
  136.         return ($ret === $tokens_of_childrentrue $ret;
  137.         
  138.     }
  139. }

Documentation generated on Thu, 19 Jun 2008 18:50:22 -0400 by phpDocumentor 1.4.2