1 : <?php
2 :
3 : /**
4 : * Definition that allows a set of elements, but disallows empty children.
5 : */
6 1 : 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 2 : if (empty($tokens_of_children)) return false;
36 :
37 : // the new set of children
38 2 : $result = array();
39 :
40 : // current depth into the nest
41 2 : $nesting = 0;
42 :
43 : // whether or not we're deleting a node
44 2 : $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 2 : $pcdata_allowed = isset($this->elements['#PCDATA']);
50 :
51 : // a little sanity check to make sure it's not ALL whitespace
52 2 : $all_whitespace = true;
53 :
54 : // some configuration
55 2 : $escape_invalid_children = $config->get('Core', 'EscapeInvalidChildren');
56 :
57 : // generator
58 2 : $gen = new HTMLPurifier_Generator($config, $context);
59 :
60 2 : foreach ($tokens_of_children as $token) {
61 2 : if (!empty($token->is_whitespace)) {
62 2 : $result[] = $token;
63 2 : continue;
64 : }
65 2 : $all_whitespace = false; // phew, we're not talking about whitespace
66 :
67 2 : $is_child = ($nesting == 0);
68 :
69 2 : if ($token instanceof HTMLPurifier_Token_Start) {
70 2 : $nesting++;
71 2 : } elseif ($token instanceof HTMLPurifier_Token_End) {
72 2 : $nesting--;
73 2 : }
74 :
75 2 : if ($is_child) {
76 2 : $is_deleting = false;
77 2 : if (!isset($this->elements[$token->name])) {
78 : $is_deleting = true;
79 : if ($pcdata_allowed && $token instanceof HTMLPurifier_Token_Text) {
80 : $result[] = $token;
81 : } elseif ($pcdata_allowed && $escape_invalid_children) {
82 : $result[] = new HTMLPurifier_Token_Text(
83 : $gen->generateFromToken($token)
84 : );
85 : }
86 : continue;
87 : }
88 2 : }
89 2 : if (!$is_deleting || ($pcdata_allowed && $token instanceof HTMLPurifier_Token_Text)) {
90 2 : $result[] = $token;
91 2 : } elseif ($pcdata_allowed && $escape_invalid_children) {
92 : $result[] =
93 : new HTMLPurifier_Token_Text(
94 : $gen->generateFromToken($token)
95 : );
96 : } else {
97 : // drop silently
98 : }
99 2 : }
100 2 : if (empty($result)) return false;
101 2 : if ($all_whitespace) return false;
102 2 : if ($tokens_of_children == $result) return true;
103 : return $result;
104 : }
105 : }
106 :
|