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 $tokens_of_children
54 * @param HTMLPurifier_Config $config
55 * @param HTMLPurifier_Context $context
58 public function validateChildren($tokens_of_children, $config, $context)
60 // Flag for subclasses
61 $this->whitespace
= false;
63 // if there are no tokens, delete parent node
64 if (empty($tokens_of_children)) {
68 // the new set of children
71 // current depth into the nest
74 // whether or not we're deleting a node
77 // whether or not parsed character data is allowed
78 // this controls whether or not we silently drop a tag
79 // or generate escaped HTML from it
80 $pcdata_allowed = isset($this->elements
['#PCDATA']);
82 // a little sanity check to make sure it's not ALL whitespace
83 $all_whitespace = true;
86 $escape_invalid_children = $config->get('Core.EscapeInvalidChildren');
89 $gen = new HTMLPurifier_Generator($config, $context);
91 foreach ($tokens_of_children as $token) {
92 if (!empty($token->is_whitespace
)) {
96 $all_whitespace = false; // phew, we're not talking about whitespace
98 $is_child = ($nesting == 0);
100 if ($token instanceof HTMLPurifier_Token_Start
) {
102 } elseif ($token instanceof HTMLPurifier_Token_End
) {
107 $is_deleting = false;
108 if (!isset($this->elements
[$token->name
])) {
110 if ($pcdata_allowed && $token instanceof HTMLPurifier_Token_Text
) {
112 } elseif ($pcdata_allowed && $escape_invalid_children) {
113 $result[] = new HTMLPurifier_Token_Text(
114 $gen->generateFromToken($token)
120 if (!$is_deleting ||
($pcdata_allowed && $token instanceof HTMLPurifier_Token_Text
)) {
122 } elseif ($pcdata_allowed && $escape_invalid_children) {
124 new HTMLPurifier_Token_Text(
125 $gen->generateFromToken($token)
131 if (empty($result)) {
134 if ($all_whitespace) {
135 $this->whitespace
= true;
138 if ($tokens_of_children == $result) {
145 // vim: et sw=4 sts=4