| 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 | public function validateChildren($tokens_of_children, $config, $context) { |
|---|
| 14 | |
|---|
| 15 | $def = $config->getHTMLDefinition(); |
|---|
| 16 | if (!$this->init) { |
|---|
| 17 | // allow all inline elements |
|---|
| 18 | $this->real_elements = $this->elements; |
|---|
| 19 | $this->fake_elements = $def->info_content_sets['Flow']; |
|---|
| 20 | $this->fake_elements['#PCDATA'] = true; |
|---|
| 21 | $this->init = true; |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | // trick the parent class into thinking it allows more |
|---|
| 25 | $this->elements = $this->fake_elements; |
|---|
| 26 | $result = parent::validateChildren($tokens_of_children, $config, $context); |
|---|
| 27 | $this->elements = $this->real_elements; |
|---|
| 28 | |
|---|
| 29 | if ($result === false) return array(); |
|---|
| 30 | if ($result === true) $result = $tokens_of_children; |
|---|
| 31 | |
|---|
| 32 | $block_wrap_start = new HTMLPurifier_Token_Start($def->info_block_wrapper); |
|---|
| 33 | $block_wrap_end = new HTMLPurifier_Token_End( $def->info_block_wrapper); |
|---|
| 34 | $is_inline = false; |
|---|
| 35 | $depth = 0; |
|---|
| 36 | $ret = array(); |
|---|
| 37 | |
|---|
| 38 | // assuming that there are no comment tokens |
|---|
| 39 | foreach ($result as $i => $token) { |
|---|
| 40 | $token = $result[$i]; |
|---|
| 41 | // ifs are nested for readability |
|---|
| 42 | if (!$is_inline) { |
|---|
| 43 | if (!$depth) { |
|---|
| 44 | if ( |
|---|
| 45 | ($token instanceof HTMLPurifier_Token_Text && !$token->is_whitespace) || |
|---|
| 46 | (!$token instanceof HTMLPurifier_Token_Text && !isset($this->elements[$token->name])) |
|---|
| 47 | ) { |
|---|
| 48 | $is_inline = true; |
|---|
| 49 | $ret[] = $block_wrap_start; |
|---|
| 50 | } |
|---|
| 51 | } |
|---|
| 52 | } else { |
|---|
| 53 | if (!$depth) { |
|---|
| 54 | // starting tokens have been inline text / empty |
|---|
| 55 | if ($token instanceof HTMLPurifier_Token_Start || $token instanceof HTMLPurifier_Token_Empty) { |
|---|
| 56 | if (isset($this->elements[$token->name])) { |
|---|
| 57 | // ended |
|---|
| 58 | $ret[] = $block_wrap_end; |
|---|
| 59 | $is_inline = false; |
|---|
| 60 | } |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | } |
|---|
| 64 | $ret[] = $token; |
|---|
| 65 | if ($token instanceof HTMLPurifier_Token_Start) $depth++; |
|---|
| 66 | if ($token instanceof HTMLPurifier_Token_End) $depth--; |
|---|
| 67 | } |
|---|
| 68 | if ($is_inline) $ret[] = $block_wrap_end; |
|---|
| 69 | return $ret; |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | |
|---|