Convert all to new configuration get/set format.
[htmlpurifier/bfroehle.git] / library / HTMLPurifier / ChildDef / Required.php
blob4889f249b8629fc71460f344e4fa8deb30d1d22f
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 * @public
12 public $elements = array();
13 /**
14 * Whether or not the last passed node was all whitespace.
16 protected $whitespace = false;
17 /**
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) {
29 $elements[$i] = true;
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
45 $result = array();
47 // current depth into the nest
48 $nesting = 0;
50 // whether or not we're deleting a node
51 $is_deleting = false;
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;
61 // some configuration
62 $escape_invalid_children = $config->get('Core.EscapeInvalidChildren');
64 // generator
65 $gen = new HTMLPurifier_Generator($config, $context);
67 foreach ($tokens_of_children as $token) {
68 if (!empty($token->is_whitespace)) {
69 $result[] = $token;
70 continue;
72 $all_whitespace = false; // phew, we're not talking about whitespace
74 $is_child = ($nesting == 0);
76 if ($token instanceof HTMLPurifier_Token_Start) {
77 $nesting++;
78 } elseif ($token instanceof HTMLPurifier_Token_End) {
79 $nesting--;
82 if ($is_child) {
83 $is_deleting = false;
84 if (!isset($this->elements[$token->name])) {
85 $is_deleting = true;
86 if ($pcdata_allowed && $token instanceof HTMLPurifier_Token_Text) {
87 $result[] = $token;
88 } elseif ($pcdata_allowed && $escape_invalid_children) {
89 $result[] = new HTMLPurifier_Token_Text(
90 $gen->generateFromToken($token)
93 continue;
96 if (!$is_deleting || ($pcdata_allowed && $token instanceof HTMLPurifier_Token_Text)) {
97 $result[] = $token;
98 } elseif ($pcdata_allowed && $escape_invalid_children) {
99 $result[] =
100 new HTMLPurifier_Token_Text(
101 $gen->generateFromToken($token)
103 } else {
104 // drop silently
107 if (empty($result)) return false;
108 if ($all_whitespace) {
109 $this->whitespace = true;
110 return false;
112 if ($tokens_of_children == $result) return true;
113 return $result;
117 // vim: et sw=4 sts=4