PSR-2 reformatting PHPDoc corrections
[htmlpurifier.git] / library / HTMLPurifier / ChildDef / Required.php
blobeaa85d4a5f17c0e94d1b6e65580aa923281385a7
1 <?php
3 /**
4 * Definition that allows a set of elements, but disallows empty children.
5 */
6 class HTMLPurifier_ChildDef_Required extends HTMLPurifier_ChildDef
8 /**
9 * Lookup table of allowed elements.
10 * @type array
12 public $elements = array();
14 /**
15 * Whether or not the last passed node was all whitespace.
16 * @type bool
18 protected $whitespace = false;
20 /**
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) {
33 $elements[$i] = true;
34 if (empty($i)) {
35 unset($elements[$i]);
36 } // remove blank
39 $this->elements = $elements;
42 /**
43 * @type bool
45 public $allow_empty = false;
47 /**
48 * @type string
50 public $type = 'required';
52 /**
53 * @param array $tokens_of_children
54 * @param HTMLPurifier_Config $config
55 * @param HTMLPurifier_Context $context
56 * @return array
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)) {
65 return false;
68 // the new set of children
69 $result = array();
71 // current depth into the nest
72 $nesting = 0;
74 // whether or not we're deleting a node
75 $is_deleting = false;
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;
85 // some configuration
86 $escape_invalid_children = $config->get('Core.EscapeInvalidChildren');
88 // generator
89 $gen = new HTMLPurifier_Generator($config, $context);
91 foreach ($tokens_of_children as $token) {
92 if (!empty($token->is_whitespace)) {
93 $result[] = $token;
94 continue;
96 $all_whitespace = false; // phew, we're not talking about whitespace
98 $is_child = ($nesting == 0);
100 if ($token instanceof HTMLPurifier_Token_Start) {
101 $nesting++;
102 } elseif ($token instanceof HTMLPurifier_Token_End) {
103 $nesting--;
106 if ($is_child) {
107 $is_deleting = false;
108 if (!isset($this->elements[$token->name])) {
109 $is_deleting = true;
110 if ($pcdata_allowed && $token instanceof HTMLPurifier_Token_Text) {
111 $result[] = $token;
112 } elseif ($pcdata_allowed && $escape_invalid_children) {
113 $result[] = new HTMLPurifier_Token_Text(
114 $gen->generateFromToken($token)
117 continue;
120 if (!$is_deleting || ($pcdata_allowed && $token instanceof HTMLPurifier_Token_Text)) {
121 $result[] = $token;
122 } elseif ($pcdata_allowed && $escape_invalid_children) {
123 $result[] =
124 new HTMLPurifier_Token_Text(
125 $gen->generateFromToken($token)
127 } else {
128 // drop silently
131 if (empty($result)) {
132 return false;
134 if ($all_whitespace) {
135 $this->whitespace = true;
136 return false;
138 if ($tokens_of_children == $result) {
139 return true;
141 return $result;
145 // vim: et sw=4 sts=4