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]); // remove blank
31 $this->elements
= $elements;
33 var $allow_empty = false;
34 var $type = 'required';
35 function validateChildren($tokens_of_children, $config, &$context) {
36 // if there are no tokens, delete parent node
37 if (empty($tokens_of_children)) return false;
39 // the new set of children
42 // current depth into the nest
45 // whether or not we're deleting a node
48 // whether or not parsed character data is allowed
49 // this controls whether or not we silently drop a tag
50 // or generate escaped HTML from it
51 $pcdata_allowed = isset($this->elements
['#PCDATA']);
53 // a little sanity check to make sure it's not ALL whitespace
54 $all_whitespace = true;
57 $escape_invalid_children = $config->get('Core', 'EscapeInvalidChildren');
62 $gen = new HTMLPurifier_Generator();
65 foreach ($tokens_of_children as $token) {
66 if (!empty($token->is_whitespace
)) {
70 $all_whitespace = false; // phew, we're not talking about whitespace
72 $is_child = ($nesting == 0);
74 if ($token->type
== 'start') {
76 } elseif ($token->type
== 'end') {
82 if (!isset($this->elements
[$token->name
])) {
84 if ($pcdata_allowed && $token->type
== 'text') {
86 } elseif ($pcdata_allowed && $escape_invalid_children) {
87 $result[] = new HTMLPurifier_Token_Text(
88 $gen->generateFromToken($token, $config)
94 if (!$is_deleting ||
($pcdata_allowed && $token->type
== 'text')) {
96 } elseif ($pcdata_allowed && $escape_invalid_children) {
98 new HTMLPurifier_Token_Text(
99 $gen->generateFromToken( $token, $config )
105 if (empty($result)) return false;
106 if ($all_whitespace) return false;
107 if ($tokens_of_children == $result) return true;