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();
15 * Whether or not the last passed node was all whitespace.
18 protected $whitespace = false;
21 * @param array|string $elements List of allowed element names (lowercase).
23 public function __construct($elements)
25 if (is_string($elements)) {
26 $elements = str_replace(' ', '', $elements);
27 $elements = explode('|', $elements);
29 $keys = array_keys($elements);
30 if ($keys == array_keys($keys)) {
31 $elements = array_flip($elements);
32 foreach ($elements as $i => $x) {
39 $this->elements
= $elements;
45 public $allow_empty = false;
50 public $type = 'required';
53 * @param array $children
54 * @param HTMLPurifier_Config $config
55 * @param HTMLPurifier_Context $context
58 public function validateChildren($children, $config, $context)
60 // Flag for subclasses
61 $this->whitespace
= false;
63 // if there are no tokens, delete parent node
64 if (empty($children)) {
68 // the new set of children
71 // whether or not parsed character data is allowed
72 // this controls whether or not we silently drop a tag
73 // or generate escaped HTML from it
74 $pcdata_allowed = isset($this->elements
['#PCDATA']);
76 // a little sanity check to make sure it's not ALL whitespace
77 $all_whitespace = true;
79 $stack = array_reverse($children);
80 while (!empty($stack)) {
81 $node = array_pop($stack);
82 if (!empty($node->is_whitespace
)) {
86 $all_whitespace = false; // phew, we're not talking about whitespace
88 if (!isset($this->elements
[$node->name
])) {
90 // XXX One of these ought to be redundant or something
91 if ($pcdata_allowed && $node instanceof HTMLPurifier_Node_Text
) {
95 // spill the child contents in
96 // ToDo: Make configurable
97 if ($node instanceof HTMLPurifier_Node_Element
) {
98 for ($i = count($node->children
) - 1; $i >= 0; $i--) {
99 $stack[] = $node->children
[$i];
107 if (empty($result)) {
110 if ($all_whitespace) {
111 $this->whitespace
= true;
118 // vim: et sw=4 sts=4