4 * Definition that allows a set of elements, but disallows empty children.
6 class HTMLPurifier_ChildDef_Required
extends HTMLPurifier_ChildDef
9 * Lookup table of allowed elements.
12 public $elements = array();
14 * Whether or not the last passed node was all whitespace.
16 protected $whitespace = false;
18 * @param $elements List of allowed element names (lowercase).
20 public function __construct($elements) {
21 if (is_string($elements)) {
22 $elements = str_replace(' ', '', $elements);
23 $elements = explode('|', $elements);
25 $keys = array_keys($elements);
26 if ($keys == array_keys($keys)) {
27 $elements = array_flip($elements);
28 foreach ($elements as $i => $x) {
30 if (empty($i)) unset($elements[$i]); // remove blank
33 $this->elements
= $elements;
35 public $allow_empty = false;
36 public $type = 'required';
37 public function validateChildren($tokens_of_children, $config, $context) {
38 // Flag for subclasses
39 $this->whitespace
= false;
41 // if there are no tokens, delete parent node
42 if (empty($tokens_of_children)) return false;
44 // the new set of children
47 // current depth into the nest
50 // whether or not we're deleting a node
53 // whether or not parsed character data is allowed
54 // this controls whether or not we silently drop a tag
55 // or generate escaped HTML from it
56 $pcdata_allowed = isset($this->elements
['#PCDATA']);
58 // a little sanity check to make sure it's not ALL whitespace
59 $all_whitespace = true;
62 $escape_invalid_children = $config->get('Core.EscapeInvalidChildren');
65 $gen = new HTMLPurifier_Generator($config, $context);
67 foreach ($tokens_of_children as $token) {
68 if (!empty($token->is_whitespace
)) {
72 $all_whitespace = false; // phew, we're not talking about whitespace
74 $is_child = ($nesting == 0);
76 if ($token instanceof HTMLPurifier_Token_Start
) {
78 } elseif ($token instanceof HTMLPurifier_Token_End
) {
84 if (!isset($this->elements
[$token->name
])) {
86 if ($pcdata_allowed && $token instanceof HTMLPurifier_Token_Text
) {
88 } elseif ($pcdata_allowed && $escape_invalid_children) {
89 $result[] = new HTMLPurifier_Token_Text(
90 $gen->generateFromToken($token)
96 if (!$is_deleting ||
($pcdata_allowed && $token instanceof HTMLPurifier_Token_Text
)) {
98 } elseif ($pcdata_allowed && $escape_invalid_children) {
100 new HTMLPurifier_Token_Text(
101 $gen->generateFromToken($token)
107 if (empty($result)) return false;
108 if ($all_whitespace) {
109 $this->whitespace
= true;
112 if ($tokens_of_children == $result) return true;
117 // vim: et sw=4 sts=4