| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /** |
|---|
| 4 | * Definition that allows a set of elements, but disallows empty children. |
|---|
| 5 | */ |
|---|
| 6 | class HTMLPurifier_ChildDef_Required extends HTMLPurifier_ChildDef |
|---|
| 7 | { |
|---|
| 8 | /** |
|---|
| 9 | * Lookup table of allowed elements. |
|---|
| 10 | * @public |
|---|
| 11 | */ |
|---|
| 12 | public $elements = array(); |
|---|
| 13 | /** |
|---|
| 14 | * @param $elements List of allowed element names (lowercase). |
|---|
| 15 | */ |
|---|
| 16 | public function __construct($elements) { |
|---|
| 17 | if (is_string($elements)) { |
|---|
| 18 | $elements = str_replace(' ', '', $elements); |
|---|
| 19 | $elements = explode('|', $elements); |
|---|
| 20 | } |
|---|
| 21 | $keys = array_keys($elements); |
|---|
| 22 | if ($keys == array_keys($keys)) { |
|---|
| 23 | $elements = array_flip($elements); |
|---|
| 24 | foreach ($elements as $i => $x) { |
|---|
| 25 | $elements[$i] = true; |
|---|
| 26 | if (empty($i)) unset($elements[$i]); // remove blank |
|---|
| 27 | } |
|---|
| 28 | } |
|---|
| 29 | $this->elements = $elements; |
|---|
| 30 | } |
|---|
| 31 | public $allow_empty = false; |
|---|
| 32 | public $type = 'required'; |
|---|
| 33 | public function validateChildren($tokens_of_children, $config, $context) { |
|---|
| 34 | // if there are no tokens, delete parent node |
|---|
| 35 | if (empty($tokens_of_children)) return false; |
|---|
| 36 | |
|---|
| 37 | // the new set of children |
|---|
| 38 | $result = array(); |
|---|
| 39 | |
|---|
| 40 | // current depth into the nest |
|---|
| 41 | $nesting = 0; |
|---|
| 42 | |
|---|
| 43 | // whether or not we're deleting a node |
|---|
| 44 | $is_deleting = false; |
|---|
| 45 | |
|---|
| 46 | // whether or not parsed character data is allowed |
|---|
| 47 | // this controls whether or not we silently drop a tag |
|---|
| 48 | // or generate escaped HTML from it |
|---|
| 49 | $pcdata_allowed = isset($this->elements['#PCDATA']); |
|---|
| 50 | |
|---|
| 51 | // a little sanity check to make sure it's not ALL whitespace |
|---|
| 52 | $all_whitespace = true; |
|---|
| 53 | |
|---|
| 54 | // some configuration |
|---|
| 55 | $escape_invalid_children = $config->get('Core', 'EscapeInvalidChildren'); |
|---|
| 56 | |
|---|
| 57 | // generator |
|---|
| 58 | static $gen = null; |
|---|
| 59 | if ($gen === null) { |
|---|
| 60 | $gen = new HTMLPurifier_Generator(); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | foreach ($tokens_of_children as $token) { |
|---|
| 64 | if (!empty($token->is_whitespace)) { |
|---|
| 65 | $result[] = $token; |
|---|
| 66 | continue; |
|---|
| 67 | } |
|---|
| 68 | $all_whitespace = false; // phew, we're not talking about whitespace |
|---|
| 69 | |
|---|
| 70 | $is_child = ($nesting == 0); |
|---|
| 71 | |
|---|
| 72 | if ($token instanceof HTMLPurifier_Token_Start) { |
|---|
| 73 | $nesting++; |
|---|
| 74 | } elseif ($token instanceof HTMLPurifier_Token_End) { |
|---|
| 75 | $nesting--; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | if ($is_child) { |
|---|
| 79 | $is_deleting = false; |
|---|
| 80 | if (!isset($this->elements[$token->name])) { |
|---|
| 81 | $is_deleting = true; |
|---|
| 82 | if ($pcdata_allowed && $token instanceof HTMLPurifier_Token_Text) { |
|---|
| 83 | $result[] = $token; |
|---|
| 84 | } elseif ($pcdata_allowed && $escape_invalid_children) { |
|---|
| 85 | $result[] = new HTMLPurifier_Token_Text( |
|---|
| 86 | $gen->generateFromToken($token, $config) |
|---|
| 87 | ); |
|---|
| 88 | } |
|---|
| 89 | continue; |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | if (!$is_deleting || ($pcdata_allowed && $token instanceof HTMLPurifier_Token_Text)) { |
|---|
| 93 | $result[] = $token; |
|---|
| 94 | } elseif ($pcdata_allowed && $escape_invalid_children) { |
|---|
| 95 | $result[] = |
|---|
| 96 | new HTMLPurifier_Token_Text( |
|---|
| 97 | $gen->generateFromToken( $token, $config ) |
|---|
| 98 | ); |
|---|
| 99 | } else { |
|---|
| 100 | // drop silently |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | if (empty($result)) return false; |
|---|
| 104 | if ($all_whitespace) return false; |
|---|
| 105 | if ($tokens_of_children == $result) return true; |
|---|
| 106 | return $result; |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | |
|---|