]> git.mxchange.org Git - friendica.git/blob - library/HTMLPurifier/ChildDef/StrictBlockquote.php
mistpark 2.0 infrasturcture lands
[friendica.git] / library / HTMLPurifier / ChildDef / StrictBlockquote.php
1 <?php
2
3 /**
4  * Takes the contents of blockquote when in strict and reformats for validation.
5  */
6 class HTMLPurifier_ChildDef_StrictBlockquote extends HTMLPurifier_ChildDef_Required
7 {
8     protected $real_elements;
9     protected $fake_elements;
10     public $allow_empty = true;
11     public $type = 'strictblockquote';
12     protected $init = false;
13
14     /**
15      * @note We don't want MakeWellFormed to auto-close inline elements since
16      *       they might be allowed.
17      */
18     public function getAllowedElements($config) {
19         $this->init($config);
20         return $this->fake_elements;
21     }
22
23     public function validateChildren($tokens_of_children, $config, $context) {
24
25         $this->init($config);
26
27         // trick the parent class into thinking it allows more
28         $this->elements = $this->fake_elements;
29         $result = parent::validateChildren($tokens_of_children, $config, $context);
30         $this->elements = $this->real_elements;
31
32         if ($result === false) return array();
33         if ($result === true) $result = $tokens_of_children;
34
35         $def = $config->getHTMLDefinition();
36         $block_wrap_start = new HTMLPurifier_Token_Start($def->info_block_wrapper);
37         $block_wrap_end   = new HTMLPurifier_Token_End(  $def->info_block_wrapper);
38         $is_inline = false;
39         $depth = 0;
40         $ret = array();
41
42         // assuming that there are no comment tokens
43         foreach ($result as $i => $token) {
44             $token = $result[$i];
45             // ifs are nested for readability
46             if (!$is_inline) {
47                 if (!$depth) {
48                      if (
49                         ($token instanceof HTMLPurifier_Token_Text && !$token->is_whitespace) ||
50                         (!$token instanceof HTMLPurifier_Token_Text && !isset($this->elements[$token->name]))
51                      ) {
52                         $is_inline = true;
53                         $ret[] = $block_wrap_start;
54                      }
55                 }
56             } else {
57                 if (!$depth) {
58                     // starting tokens have been inline text / empty
59                     if ($token instanceof HTMLPurifier_Token_Start || $token instanceof HTMLPurifier_Token_Empty) {
60                         if (isset($this->elements[$token->name])) {
61                             // ended
62                             $ret[] = $block_wrap_end;
63                             $is_inline = false;
64                         }
65                     }
66                 }
67             }
68             $ret[] = $token;
69             if ($token instanceof HTMLPurifier_Token_Start) $depth++;
70             if ($token instanceof HTMLPurifier_Token_End)   $depth--;
71         }
72         if ($is_inline) $ret[] = $block_wrap_end;
73         return $ret;
74     }
75
76     private function init($config) {
77         if (!$this->init) {
78             $def = $config->getHTMLDefinition();
79             // allow all inline elements
80             $this->real_elements = $this->elements;
81             $this->fake_elements = $def->info_content_sets['Flow'];
82             $this->fake_elements['#PCDATA'] = true;
83             $this->init = true;
84         }
85     }
86 }
87
88 // vim: et sw=4 sts=4