3 require_once 'HTMLPurifier/ChildDef.php';
6 * Definition that allows a set of elements, but disallows empty children.
8 class HTMLPurifier_ChildDef_Required
extends HTMLPurifier_ChildDef
11 * Lookup table of allowed elements.
14 var $elements = array();
16 * @param $elements List of allowed element names (lowercase).
18 function HTMLPurifier_ChildDef_Required($elements) {
19 if (is_string($elements)) {
20 $elements = str_replace(' ', '', $elements);
21 $elements = explode('|', $elements);
23 $keys = array_keys($elements);
24 if ($keys == array_keys($keys)) {
25 $elements = array_flip($elements);
26 foreach ($elements as $i => $x) {
28 if (empty($i)) unset($elements[$i]);
31 $this->elements
= $elements;
32 $this->gen
= new HTMLPurifier_Generator();
34 var $allow_empty = false;
35 var $type = 'required';
36 function validateChildren($tokens_of_children, $config, &$context) {
37 // if there are no tokens, delete parent node
38 if (empty($tokens_of_children)) return false;
40 // the new set of children
43 // current depth into the nest
46 // whether or not we're deleting a node
49 // whether or not parsed character data is allowed
50 // this controls whether or not we silently drop a tag
51 // or generate escaped HTML from it
52 $pcdata_allowed = isset($this->elements
['#PCDATA']);
54 // a little sanity check to make sure it's not ALL whitespace
55 $all_whitespace = true;
58 $escape_invalid_children = $config->get('Core', 'EscapeInvalidChildren');
60 foreach ($tokens_of_children as $token) {
61 if (!empty($token->is_whitespace
)) {
65 $all_whitespace = false; // phew, we're not talking about whitespace
67 $is_child = ($nesting == 0);
69 if ($token->type
== 'start') {
71 } elseif ($token->type
== 'end') {
77 if (!isset($this->elements
[$token->name
])) {
79 if ($pcdata_allowed && $token->type
== 'text') {
81 } elseif ($pcdata_allowed && $escape_invalid_children) {
82 $result[] = new HTMLPurifier_Token_Text(
83 $this->gen
->generateFromToken($token, $config)
89 if (!$is_deleting ||
($pcdata_allowed && $token->type
== 'text')) {
91 } elseif ($pcdata_allowed && $escape_invalid_children) {
93 new HTMLPurifier_Token_Text(
94 $this->gen
->generateFromToken( $token, $config )
100 if (empty($result)) return false;
101 if ($all_whitespace) return false;
102 if ($tokens_of_children == $result) return true;