HTMLPurifier 4.4.0
/home/ezyang/Dev/htmlpurifier/library/HTMLPurifier/ChildDef/StrictBlockquote.php
Go to the documentation of this file.
00001 <?php
00002 
00006 class HTMLPurifier_ChildDef_StrictBlockquote extends HTMLPurifier_ChildDef_Required
00007 {
00008     protected $real_elements;
00009     protected $fake_elements;
00010     public $allow_empty = true;
00011     public $type = 'strictblockquote';
00012     protected $init = false;
00013 
00018     public function getAllowedElements($config) {
00019         $this->init($config);
00020         return $this->fake_elements;
00021     }
00022 
00023     public function validateChildren($tokens_of_children, $config, $context) {
00024 
00025         $this->init($config);
00026 
00027         // trick the parent class into thinking it allows more
00028         $this->elements = $this->fake_elements;
00029         $result = parent::validateChildren($tokens_of_children, $config, $context);
00030         $this->elements = $this->real_elements;
00031 
00032         if ($result === false) return array();
00033         if ($result === true) $result = $tokens_of_children;
00034 
00035         $def = $config->getHTMLDefinition();
00036         $block_wrap_start = new HTMLPurifier_Token_Start($def->info_block_wrapper);
00037         $block_wrap_end   = new HTMLPurifier_Token_End(  $def->info_block_wrapper);
00038         $is_inline = false;
00039         $depth = 0;
00040         $ret = array();
00041 
00042         // assuming that there are no comment tokens
00043         foreach ($result as $i => $token) {
00044             $token = $result[$i];
00045             // ifs are nested for readability
00046             if (!$is_inline) {
00047                 if (!$depth) {
00048                      if (
00049                         ($token instanceof HTMLPurifier_Token_Text && !$token->is_whitespace) ||
00050                         (!$token instanceof HTMLPurifier_Token_Text && !isset($this->elements[$token->name]))
00051                      ) {
00052                         $is_inline = true;
00053                         $ret[] = $block_wrap_start;
00054                      }
00055                 }
00056             } else {
00057                 if (!$depth) {
00058                     // starting tokens have been inline text / empty
00059                     if ($token instanceof HTMLPurifier_Token_Start || $token instanceof HTMLPurifier_Token_Empty) {
00060                         if (isset($this->elements[$token->name])) {
00061                             // ended
00062                             $ret[] = $block_wrap_end;
00063                             $is_inline = false;
00064                         }
00065                     }
00066                 }
00067             }
00068             $ret[] = $token;
00069             if ($token instanceof HTMLPurifier_Token_Start) $depth++;
00070             if ($token instanceof HTMLPurifier_Token_End)   $depth--;
00071         }
00072         if ($is_inline) $ret[] = $block_wrap_end;
00073         return $ret;
00074     }
00075 
00076     private function init($config) {
00077         if (!$this->init) {
00078             $def = $config->getHTMLDefinition();
00079             // allow all inline elements
00080             $this->real_elements = $this->elements;
00081             $this->fake_elements = $def->info_content_sets['Flow'];
00082             $this->fake_elements['#PCDATA'] = true;
00083             $this->init = true;
00084         }
00085     }
00086 }
00087 
00088 // vim: et sw=4 sts=4